Java having three type of control statement.Firstly we will discuss about the first one that is THE IF STATEMENT.It is similer like c/c++.The behaviour and syntax is same as c/c++ have.Mainly if statement is used for checking the condition as you all know.Below we have written the syntax  of If statement.;



syntax–>>


if(condition){ statement;}


Let’s first we understand the syntax
mainly for numbers that statement is used,
Mostly three type of operators are used inside the condition between operands.


1.’ > ‘ – first value is Greater

2.’ < ‘ – first value is less

3. ‘ == ‘ – Both are equal





syntax example–>>



if(5>4){System.out.println(” First is big “);}


above statement just check the condition and if it become true it goes inside the curly bracket.



Below is proper example with if statement in java..

Problem–>



Write a java program to execute if statement three times with using two values.



Code–>



class threeif
{
public static void main(String args[])
{
int a=10;
int b=20;
if(a<b)
{
System.out.println(“a is small”);
}
a=a*a;
if(a>b)
{
System.out.println(“a is big”);
}
b=b*5;
if(a==b)
{
System.out.println(“a and b both are equal”);
}
}
}

output–>>



a is smalla is biga and b both are equal



Logic–>>

There is nothing new. First we have initialized two variables a and b and given the values 10 and 20, and checks the first condition.As we discussed above there are mainly three operators used between the operand .The first condition checks that is it true or not.so inside the first if(a<b) becomes true so it prints “a is small” .Now we have increased the value of a by a=a*a operation,so a becomes high than b.Now in the second if statement we have checked if(a>b) and it becomes true so it prints “a is big” and at the end we did b=b*5; so it becomes 100 which is equal to new value of a,So in third condition if(a==b) it becomes true and it prints “a and b both are equal”.


So this is the first control statement with if statement in java, If you face any problem while executing above program then write your query in comment box.
Enjoy coding !

By jigar

Leave a Reply

Your email address will not be published. Required fields are marked *