If statement of Java executes a block of code according to the condition. It checks the condition. If the condition is true, it executes the If block, and if the condition is false, the If block will not be executed.
Syntax
if(condition)
{
// code
}
Flowchart
Example 1:
public class Test {
public static void main(String[] args) {
int n = 12; //Defining a variable n
if(n%2==0) // checking the condition
{
System.out.println(n+" "+"is even number");
}
if(n%2!=0) // checking the condition
{
System.out.println(n+" "+"is odd number");
}
}
}
Output
12 is even number
Example 2:
public class Test {
public static void main(String[] args) {
int m = 12; //Defining a variable m
int n = 10; //Defining a variable n
if(m>n) // checking the condition
{
System.out.println(m+" is greater than "+n);
}
if(n>m) // checking the condition
{
System.out.println(n+" is greater than "+m);
}
if(n==m) // checking the condition
{
System.out.println(n+" is equals to "+m);
}
}
}
Output
12 is greater than 10