If statements in Java

If statements in Java is used to control the program flow based on some condition, it’s used to execute some statement code block if expression is evaluated to true, otherwise it will get skipped. This is an simplest way to modify the control flow of the program.
There are various types of if statement in java.
  • if statement
  • if-else statement
  • nested if statement
  • if-else-if ladder
  • Java IF Statement

    The Java if statement tests the condition. It executes the if block if condition is true.

    syntax:

    if(test_expression)
    {
        statement 1;
        statement 2;
        ...
    }
    ‘Statement n’ can be a statement or a set of statements and if the test expression is evaluated to true, the statement block will get executed or it will get skipped.


    Example of If statements

    public class Sample{
    public static void main(String args[]){
    int a=15, b=20;
    if(b>a)
    System.out.println("b is greater");
          }
    }

    Output

    b is greater

    IF-else Statement in Java

    If else statements in Java is also used to control the program flow based on some condition, only the difference is: it’s used to execute some statement code block if expression is evaluated to true, otherwise executes else statement code block.
    The Java if-else statement also tests the condition. It executes the if block if condition is true otherwise else block is executed.

    syntax:

    
    if(test_expression)
    {
       //execute your code
    }
    else
    {
       //execute your code
    }
    flow diagram of IF-else statement

    Example of IF-else statements

    public class IfElseExample {  
    public static void main(String[] args) {  
    int number=11;  
    if(number%2==0){  
    System.out.println("even number");  
    }
    else  {  
    System.out.println("odd number");  
        }  
       }  
       }
    

    Output

    odd number

    IF-else-if ladder Statement in Java

    The if-else-if ladder statement executes one condition from multiple statements.

    syntax:

    if(condition1){  
        //code to be executed if condition1 is true  
      } else if(condition2){  
         //code to be executed if condition2 is true  
       }  
    else if(condition3){  
         //code to be executed if condition3 is true  
        }  
    ...  
    else   {  
       //code to be executed if all the conditions are false  
       }
    flow diagram of IF-else-if ladder statement

    Example of IF-else-if ladder statements

    public class IfElseIfExample {  
    public static void main(String[] args) {  
    int marks=75;  
    if(marks<70){  
    System.out.println("fail");  
        }  
    else if(marks>=50 && marks<65){  
    System.out.println("D grade");  
        }  
    else if(marks>=60 && marks<70){  
    System.out.println("C grade");  
        }  
    else if(marks>=70 && marks<80){  
    System.out.println("B grade");  
        }  
    else if(marks>=80 && marks<90){  
    System.out.println("A grade");  
        }   
    else if(marks>=90 && marks<100){  
    System.out.println("A+ grade");  
        }   else{  
    System.out.println("Invalid!");  
      }  
       }  
       }  
    

    Output

    B grade