Nested If in Java Programming

If we place If Statement inside another if block is called Nested If in Java Programming. The Java If Else statement allows us to print different statements depending upon the expression result (TRUE, FALSE). Sometimes we have to check further even when the condition is TRUE. In these situations, we can use Java Nested IF statements, but please be careful while using them.

For example, every person is qualified to work if he is 18 years old or above else, he is not qualified. However, companies will not give a post to each person. So, we use another IF Statement, also called as Java Nested If Statement, to verify his education skills or any special company requirements. Before we get into the example, let us see the syntax of Nested If.

Nested If Syntax

The Nested If in Java Programming language is as follows:

if ( test condition 1)  {
     
     //If the test condition 1 is TRUE then, it will check for test condition 2
     if ( test condition 2)  {
          //If the test condition 2 is TRUE, these statements will be executed
          Test condition 2 True statements;
     }
     else  {
          //If the test condition 2 is FALSE, these lines will be executed
          Test condition 2 False statements;
     }
else  {
     //If the test condition 1 is FALSE then these lines will be executed
     Test condition 1 False statements;
}

Nested If Flow Chart

The following picture will show the flow chart of the Java Nested If statement.

FLOW CHART For Nested If

The execution flow of the Nested If statement is

  • If Condition1 is FALSE, then STATEMENT3 will execute.
  • If Test Condition1 is TRUE, it will check for the Test Condition2
    • expression is TRUE, then STATEMENT1 will execute
    • Else STATEMENT2 executed.

Nested If in Java Programming Example

The Nested If else program allows users to enter his / her age, and we are going to store it in the variable age. If the given age is less than 18, we are going to print two statements. When the condition fails, we will check one more condition (Nested), and if it succeeds, we print something. If the expression result fails, we print some other thing.

Please refer to the If Else and IF Condition articles in Java Programming.

package ConditionalStatements;

import java.util.Scanner;

public class NestedIf {
	private static Scanner sc;
	
	public static void main(String[] args) {
		int age;
		sc = new Scanner(System.in);		
		System.out.println(" Please Enter you Age: ");
		age = sc.nextInt();
		
		if (age < 18) {
			System.out.println("You are Minor."); 
			System.out.println("You are Not Eligible to Work");
		}
		else  {
			if (age >= 18 && age <= 60 )  {
				System.out.println("You are Eligible to Work");
				System.out.println("Please fill in your details and apply");
			}
			else  {
				System.out.println("You are too old to work as per the Government rules");
				System.out.println("Please Collect your pension!");
			}
		}
		System.out.println("\nThis Message is coming from Outside the IF ELSE STATEMENT");
	}
}

Within this Java nested if else program, If the person’s age is less than 18, then he is not eligible to work. If the person’s age is greater than or equal to 18, then the first expression fails. It will check the else statement. Within the Else statement, there is another if condition (called Nested If).

  • It will check whether the person’s age is greater than or equal to 18 and less than or equal to 60. If the expression is evaluated to TRUE, then he can apply for the job.
  • If the Nested If condition is FALSE, then he is too old to work as per the government.
  • We also place one System.out.println function outside the If Else block, which will execute irrespective of the expression result.

OUTPUT 1: From the nested if statement screenshot below, you can observe that We entered the age of 16. Here, the first If condition is TRUE. So, the statements inside the first if block will execute.

Nested If in Java Programming 1

We are going to enter age as 25 means the first IF expression is FALSE. It will go to else block. Within the else block, Javac will check the if (age>= 18 && age<=60), which is TRUE. So it will print the code inside this block.

Please Enter you Age:
25
You are Eligible to Work
Please fill in your details and apply

This Message is coming from Outside the IF ELSE STATEMENT

3rd OUTPUT: This time, we are going to enter age as 61 to test the Nested If. It means the first IF condition is FALSE. It will go to else block. Within the else block, the Javac compiler will check the if (age>= 18 && age<=60), which is FALSE. That is why this program will print the code inside the sub else block.

Please Enter you Age:
61
You are too old to work as per the Government rules
Please Collect your pension!

This Message is coming from Outside the IF ELSE STATEMENT