If-else-if ladder Statement of Java

The if-else-if ladder contains multiple conditions. It checks the conditions one by one from top to bottom. Once any condition is found true, It executes the code of that condition and leaves the remaining conditions. Finally, it executes the code of only one condition. If any condition is not true, then it executes the code of else part.

Syntax
    if(condition1)
     {  
       //This code will be executed if condition1 is true  
     }
    else if(condition2)
     {  
       //This code will be executed if condition2 is true  
     }  
    else if(condition3)
     {  
       //This code will be executed if condition3 is true  
     }  
    ...  // more conditions here
    ...
    else
     {  
       //This code will be executed if all conditions are false 
     }  
Flowchart
Example 1:
// This program will print the largest number out of the three numbers
public class Test {
 
    public static void main(String[] args) {
         
        int x = 13; //Defining a variable x
        int y = 15; //Defining a variable y
        int z = 16; //Defining a variable z
        if(x>y && x>z) // checking the condition
        {
        	// will execute this code if this condition is true
            System.out.println(x+" "+"is the largest number");
        }
        else if(y>x && y>z) // checking the condition
        {
        	// will execute this code if this condition is true
        	System.out.println(y+" "+"is the largest number");        
        } 
        else 
        {
        	// will execute this code if all conditions are false
        	System.out.println(z+" "+"is the largest number"); 
        }
    }
 
}
Output
16 is the largest number
Example 2:
// This program will print the grade of the Student based on marks.
public class Test {
 
    public static void main(String[] args) {
         
        int examMarks = 92; //Defining a variable examMarks
     
        if(examMarks > 90 && examMarks <=100) // checking the condition
        {
        	// will execute this code if this condition is true
            System.out.println("A Grade");
        }
        else if(examMarks > 80 && examMarks <=90) // checking the condition
        {
        	// will execute this code if this condition is true
        	System.out.println("Grade B");        
        } 
        else if(examMarks > 70 && examMarks <=80) // checking the condition
        {
        	// will execute this code if this condition is true
        	System.out.println("Grade C");        
        } 
        else if(examMarks >= 60 && examMarks <=70) // checking the condition
        {
        	// will execute this code if this condition is true
        	System.out.println("Grade D");        
        } 
        else if(examMarks >=0 && examMarks <60) // checking the condition
        {
        	// will execute this code if this condition is true
        	System.out.println("Fail");        
        } 
        else 
        {
        	// will execute this code if all conditions are false
        	System.out.println("Invalid Marks, Please enter corrrect marks"); 
        }
    }
 
}
Output
A Grade

Leave a Comment