Nested If In Java: Syntax, Flowchart, and Practical Examples

Java supports nested if and nested if-else. In a programming language, the basic and simple conditional statement is if statement.

But nested if is different and more complex than other conditional statements.

What is nested if in Java?

A nested-if is an if statement placed inside another if statement. This means, that if one if statement contains another if statement then it is called a nested if statement.

And if One if statement contains if-else statement then it is called a nested if-else statement.

We can place N number of if-else statements inside a nested if. There is no strict limit to the number of if-else statements you can place within a nested if structure in Java. You can nest as many if-else statements as needed, based on the logic of your program.

Syntax

Here’s the basic syntax for a nested if statement in Java:

if (condition1) {
    // Executes when condition1 is true
    if (condition2) {
        // Executes when both condition1 and condition2 are true
        // Additional code here
    } else {
        // Executes when condition1 is true but condition2 is false
    }
} else {
    // Executes when condition1 is false
}

Flowchart

nested if in java flowchart

Explanation

let’s understand the concept with the help of a flowchart:

  1. Start: The control flow begins at the top of the flowchart.
  2. Check Condition: The first if condition is checked.
    • If the Condition is False, the flow moves to the Else Block, where the corresponding code is executed. After execution, control transfers to the Next Statement in the Program.
    • If the Condition is True, the flow proceeds to the next decision node to check the Nested If Condition.
  3. Check Nested If Condition: If the initial condition is true, the nested if condition is evaluated.
    • If the Nested If Condition is True, the flow moves to the Nested If Block, where the specific code is executed. After execution, control moves to the Next Statement in the Program.
    • If Nested If Condition is False, the flow moves to the Nested Else Block, where the alternative code is executed. After execution, control also transfers to the Next Statement in the Program.

Program Example 1: Checking Eligibility for a Discount

public class DiscountEligibilityDemo {
    public static void main(String[] args) {
        int age = 41;
        boolean isMember = true;

        if (age >= 18) {
            if (age > 59) {
                System.out.println("******** Senior Citizen ********");
                System.out.println("Eligible for senior citizen discount.");
            } else {
                if (isMember) {
                    System.out.println("Eligible for member discount.");
                    System.out.println("Yes, the person is a member.");
                } else {
                    System.out.println("Not eligible for any discount.");
                    System.out.println("No, the person is not a member.");
                }
            }
        } else {
            System.out.println("******** Not an Adult ********");
            System.out.println("Not eligible for adult discounts.");
        }
    }
}

Explanation

  1. Age Check (Outer If):
    • The outer if statement checks if the person’s age is 18 or above.
    • If True, it moves to check if the person is a senior citizen.
  2. Senior Citizen Check (Nested If):
    • If the age is greater than 59, the person is considered a senior citizen and eligible for the senior citizen discount.
  3. Membership Check (Nested If):
    • If the person is not a senior citizen (age between 18 and 59), the code checks if they are a member.
    • If True, they are eligible for a member discount.
    • If False, they are not eligible for any discount.
  4. Age Below 18 (Else Block):
    • If the initial age check fails (age < 18), the person is not eligible for adult discounts.

Output

Output
Eligible for member discount.
Yes, the person is a member.

Program Example 2: Determine Fitness Category

public class FitnessCategory {
    public static void main(String[] args) {
        int age = 30;
        int heightInCm = 165;

        if (age > 18) {
            if (heightInCm >= 170) {
                System.out.println("Category: Adult Fit");
            } else {
                System.out.println("Category: Active Adult");
            }
        } else {
            if (heightInCm >= 160) {
                System.out.println("Category: Young Fit");
            } else {
                System.out.println("Category: Youth Active");
            }
        }
    }
}

Explanation

  1. Age Check (Outer If):
    • The outer if statement checks if the person is older than 18.
    • If True, it proceeds to check the height to determine the fitness category for adults.
  2. Height Check for Adults (Nested If):
    • If the person is older than 18 and their height is 170 cm or more, they are categorized as “Adult Fit.”
    • If the height is less than 170 cm, they are categorized as “Active Adult.”
  3. Age Below 18 (Else Block):
    • If the person is 18 or younger, it checks the height.
      • If the height is 160 cm or more, they are categorized as “Young Fit.”
      • If the height is less than 160 cm, they are categorized as “Youth Active.”

Output

Output
Category: Active Adult

Tip: Simplify Nested if Statements

Avoid Deep Nesting: While nested if statements can be useful for checking multiple conditions, excessive nesting can make code hard to read and maintain. Instead of deeply nesting multiple if statements, consider using logical operators (like && and ||) to combine conditions where possible. This approach simplifies your code and makes it more readable.

Conclusion

Nested if statements are a powerful tool in Java for handling complex decision-making scenarios. By placing if statements within other if statements, you can evaluate multiple conditions in a hierarchical manner, providing greater control and precision. Mastering nested if structures will help you write more flexible and organized code.

We’d Love to Hear From You!

Do you have questions or feedback about nested if statements in Java? We’re here to assist you! Whether you need further clarification, have suggestions, or just want to share your thoughts, please leave a comment below. Your feedback is invaluable in helping us improve our content and support others. Don’t hesitate to reach out—we’re excited to hear from you!

Explore More Java Topics

Interested in expanding your knowledge of Java control structures? Check out our related posts:

  • If Else in Java – Dive into the If Else statement and understand its practical uses.
  • If Statement in Java – Explore the basics of the If statement and how to implement it effectively.
  • If Else If in Java – Learn about the If Else If ladder for handling multiple conditions.

Feel free to explore these articles to deepen your understanding of Java programming!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
Aitechray