if else ladder in java

We have discussed the if else statement and also discussed use of it. In this post, we will discuss the if else if ladder and also see the flowchart of it. In if else statement, the if block of statements will be executed if the given condition is true otherwise else block of the statement, will be executed. In if else statement the block of statements will be executed if the given condition is true otherwise block of the statement will be skipped. If we want to execute the different codes based on different conditions then we can use if-else-if. It is also known as if else if ladder statement in java. This statement is always be executed from the top down. During the execution of conditions if any condition founds true, then the statement associated with that if it is executed, and the rest of the code will be skipped. If none of the conditions is true, then the final else statement will be executed.

if(condition)
{
   // If condition is true then this block of statements will be executed
}
else if(condition)
{
   // If condition is true then this block of statements will be executed
}
.
.
.
else 
{
   // If none of condition is true, then this block of statements will be executed
}

Flowchart of if else if ladder statement in java

Flowchart of if else if ladder statement in java
class ExampleIfElseIfStatement 
{ 
   public static void main(String args[]) 
   { 
      int age = 15; 
      if(age < 18) 
      {
	System.out.println("The age of person is : " + age + " He/she is a child."); 
      } 
      else if(age > 18 && age < 40)
      {	
 	System.out.println("The age of person is : " + age + " He/she is Younger."); 
      }
      else 
      {
	System.out.println("The age of person is : " + age + " He/she is old person.");     	 
      }
      System.out.println("This statement is outside of the if-else-if");
   } 
}

Output: The age of person is : 15 He/she is a child.
This statement is outside of the if-else-if

In Java, if statement having an optional block and this block is known as else block. You must think why we are saying else block is optional? Because if block can be used without else block but else block exists only with if block. The statements inside of else block will be executed only if the test expression is evaluated to false. An if statement can be followed by an optional else if also.

Important point

1. The if block can be followed by either zero or one else block or if block.
2. If any of else if block succeeds, none of the remaining will be tested.

1. Quiz, Read the below code and do answer.

public class ExampleIfStatement
{  
     public static void main(String[] args)
     {
    	int a = 10;
    	if(a > 5)
    	{
    		System.out.println("A is greter than 5");
    	}
    	else if(a < 7)
    	{
    		System.out.println("A is less than 7");
    	}
    	else 
    		System.out.println("A is greter than 7");
    	if(a > 8)
    		System.out.println("A is greter than 8");
     }  
}  

Click on anyone to know the answer.

2. Quiz, Read the below code and do answer.

public class ExampleIfStatement
{  
     public static void main(String[] args)
     {
    	String website = "java";
    	if(website.equals("java"))
    	{
    		System.out.println("JavaGoal.com");
    	}
    	else if(website.equals("final"))
    	{
    		System.out.println("FinalRope.com");
    	}
    	
    	System.out.println("Leraning website");
    	
     }  
}  

Click on anyone to know the answer.

3. Quiz, Read the below code and do answer.

public class ExampleIfStatement
{  
     public static void main(String[] args)
     {
    	String website = "java";
    	if(website.equals("java"))
    	{
    		System.out.println("JavaGoal.com");
    	}
    	else if(website.equals("final"))
    	{
    		System.out.println("FinalRope.com");
    	}
    	else
    		System.out.println("Leraning website");
     }  
}  

Click on anyone to know the answer.

4. Quiz, Read the below code and do answer.

public class ExampleIfStatement
{  
     public static void main(String[] args)
     {
    	 int a = 5, b = 10, c = 8;  
    	 if(a > b && a > c)
    		System.out.println("a is greatest");
    	 else if (b > a && b > c) 
    		System.out.println("b is greatest");
    	 else
     		System.out.println("c is greatest");
     }  
}  

Click on anyone to know the answer.

5. Quiz, Read the below code and do answer.

public class ExampleIfStatement
{  
     public static void main(String[] args)
     {
    	 int a = 85;
    	 if(a < 35)
    		 System.out.println("Fail");
    	 else if(a > 35 && a < 50)
    		 System.out.println("Pass");
    	 else if(a > 50 && a < 75)
    		 System.out.println("Second");
    	 else
    		 System.out.println("First");
     }  
}  

Click on anyone to know the answer.

Leave a Comment