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–>>
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–>
{
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–>>
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 !