KnowledgeBoat Logo

Conditional Statements in Java

Nested ifs

ICSE Computer Applications


We can write an if-else statement within another if-else statement. We call this nested if. Let’s look at an example of nested if.

In our nested if-else example program, we will ask the user to enter his or her age. Our program should tell the user if he is an adult or not. If the user is not an adult, our program should tell the user if he is a minor or teenager. If the user is an adult, our program should tell the user if he is a senior citizen. The age categories are defined in this table.

AgeCategory
0-12Minor
13-17Teenager
18-59Adult
60 and aboveSenior Citizen

This a sample output of our program for different ages.

Sample Output of Java age category program

Here is our Java program:

/**
 * This program shows the usage of nested if-else statements.
 *
 * @author KnowledgeBoat
 * @version 1.0
 */

import java.util.Scanner;

public class KboatAgeCategory
{
    public void ageCategory() {
        Scanner in = new Scanner(System.in);
        
        System.out.print("Enter your age: ");
        int age = in.nextInt();
        
        if (age < 18) {
            System.out.println("You are not an adult");
            if (age < 13)
                System.out.println("You are a minor");
            else
                System.out.println("You are a teenager");
        }
        else {
            System.out.print("You are an adult");
            if (age >= 60) 
                System.out.println(" and a senior citizen");
            else
                System.out.print('\n');
        }
    }
}

Our program makes use of nested if-else statements. In the outer if-else, we check if the user is an adult or not and display an appropriate message.

if (age < 18) {
    System.out.println("You are not an adult");
    if (age < 13)
        System.out.println("You are a minor");
    else
        System.out.println("You are a teenager");
}

If the user is not an adult, program control will enter the if part of the outer if-else statement shown above and the inner if-else will check if the user is a minor or teenager.

else {
    System.out.print("You are an adult");
    if (age >= 60) 
        System.out.println(" and a senior citizen");
    else
        System.out.print('\n');
}

If the user is an adult, program control will go to the else part of the if-else statement shown above. The inner if-else will check if the user is a senior citizen or not.

This is how we can use nested if-else statements to do conditional checks at multiple levels. Below is the output of the program:

BlueJ output of Special number Java program for ICSE Computer Applications course

Pairing of nested if-else

At this point, I want to tell you an important thing about nested ifs. When you use nested ifs, remember that an else statement always refers to the nearest if statement that is within the same code block as the else and that is not already associated with an else.

In the if else pairing example program shown above, the arrows show the association of different else with their corresponding if statements. Below is the output of the program:

BlueJ output of if else pairing example Java program for ICSE Computer Applications course
PrevNext