Nested if statement in Java

We have discussed the if statement in recent posts. In this post, we see how we can use the nested if statement in java and nested if java. There are a number of situations when we want to execute the code based on conditions. The nested if statement in java means an if statement inside an if statement. The contained if statement is known as inner if statement and another is known as outer if statement. The inner block of if statement will be executed only if the outer block condition is true. In Java, We can use nested if statements, It means if statement within if statements. i.e, we can place any number of if statement inside another if statement. But it may increase the complexity of your program.

Syntax of nested if statement

if statement in java

Flowchart

Flowchart of Nested if statement in Java
class ExampleNestedIfStatement 
{ 
   public static void main(String args[]) 
   { 
      int age = 20; 
      boolean hasVoterCard = true;
      if (age > 18)  // if age is greater than 18
      {  
         // It will be print when outer if block condition is true. 
         // It doesn't depends on the inner if block.	
	 System.out.println("The age of person is : " + age); 
	 if(hasVoterCard) // If person has voter card
	 {
	    // It will be print only if outer and inner block condition both are true 
	    System.out.println("The person has voter card also");  
	 } // inner if block is closed
		  
         // It will be print when outer if block condition is true. 
         // It doesn't depends on the inner if block .
	 System.out.println("Statement after the inner if block"); 
      }  // outer if block is closed
 
      // It will print always because it outside the both if blocks.
      System.out.println("Statement after the outer if block"); 
   } 
}

Output:
The age of person is : 20
The person has voter card also
Statement after the inner if block
Statement after the outer if block
Another example of Nest if with different scenario:

class ExampleNestedIfStatement 
{ 
   public static void main(String args[]) 
   { 
 	int age = 20; 
	boolean hasVoterCard = false;
	if (age > 18)  // if age is greater than 18
	{
	    // It will be print when outer if block condition is true. 
            // It doesn't depends on the inner if block
	    System.out.println("The age of person is : " + age); 
	    if(hasVoterCard) // If person has voter card
	    {
	        // It will be print only if outer and inner block condition both are true 
	  	 System.out.println("The person has voter card also");  
	    } // inner if block closed
		  
	    // It will be print when outer if block condition is true.
            // It doesn't depends on the inner if block. 
            System.out.println("Statement after the inner if block"); 
	} // outer if block closed	 
	// It will print always because it outside the both if blocks.
	System.out.println("Statement after the outer if block"); 
   } 
}

Output: The age of person is : 20
Statement after the inner if block
Statement after the outer if block

In the above example, A person is eligible for a vote only if he/she is above 18 and having a voter card. So, first of all we need to check the age of the person and after that, we will verify the person have a voter card or not.
So Here are using nested if statement.
The outer if statement condition is checking, Is person is above 18 or not?
The inner if statement condition is checking, Is a person having a voter card or not?
The inner if statement is totally dependent on the outer if statement. If a person is less than 18 then not need to check the voter card.

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

public class ExampleIfStatement
{  
     public static void main(String[] args)
     {  
    	 if(true)
    		 if(true)
    			 System.out.println("If statement has executed");   		 
     }  
 }

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)
   {
      int a = 10, b = 8, c = 5;
      if(a > b)
         if(b > c)
            System.out.println("a is the greatest value");
   }
}

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)
   {
      if(false)
         System.out.println("First if has executed");
      if(true)
         System.out.println("Second if has executed");

      System.out.println("Outside the if Nested If");
   }
}

Click on anyone to know the answer.

Leave a Comment