Lesson 22 of 50 – Java Nested If Statements
44%

Java Nested If Statements

A nested if statement means placing one if statement inside another if statement. It is useful when a program must check multiple related conditions step by step.

1. What Is a Nested If Statement?

In a nested if statement, the inner condition is checked only when execution reaches the inner if. Commonly, this happens after the outer condition is true.

Syntax:

if (condition1) {
    if (condition2) {
        // Code executes when both conditions are true
    }
}

Example:

public class Main {
    public static void main(String[] args) {
        int age = 20;

        if (age >= 18) {
            if (age >= 18) {
                System.out.println("You are an adult.");
            }
        }
    }
}
Output:
You are an adult.

Both conditions are true, so the message is displayed.

2. Nested If with Two Conditions

We can check two different conditions using nested if statements.

public class Main {
    public static void main(String[] args) {
        int age = 22;
        boolean hasID = true;

        if (age >= 18) {
            if (hasID) {
                System.out.println("Entry allowed.");
            }
        }
    }
}
Output:
Entry allowed.

The outer condition checks the age. If it is true, the inner condition checks whether the person has an ID.

3. Nested If-Else Statement

You can use else inside a nested if to handle a false inner condition.

public class Main {
    public static void main(String[] args) {
        int age = 20;
        boolean hasID = false;

        if (age >= 18) {
            if (hasID) {
                System.out.println("Entry allowed.");
            } else {
                System.out.println("Please show your ID.");
            }
        } else {
            System.out.println("You are too young.");
        }
    }
}
Output:
Please show your ID.

The outer condition is true, but the inner condition is false. Therefore, the inner else runs.

4. Checking Student Pass or Fail

A nested if can check whether a student has passed and then determine the grade.

public class Main {
    public static void main(String[] args) {
        int marks = 85;

        if (marks >= 40) {
            System.out.println("You have passed.");

            if (marks >= 80) {
                System.out.println("Grade A");
            } else {
                System.out.println("Grade B");
            }

        } else {
            System.out.println("You have failed.");
        }
    }
}
Output:
You have passed.
Grade A

First, Java checks whether marks are at least 40. If the student passes, Java checks the grade.

5. Checking Positive Number and Even Number

Nested if statements can check whether a number is positive and whether it is even.

public class Main {
    public static void main(String[] args) {
        int number = 12;

        if (number > 0) {
            System.out.println("Positive number");

            if (number % 2 == 0) {
                System.out.println("Even number");
            } else {
                System.out.println("Odd number");
            }

        } else {
            System.out.println("Number is not positive.");
        }
    }
}
Output:
Positive number
Even number

The even-or-odd condition is checked only when the number is positive.

6. Nested If with Three Conditions

You can place multiple nested if statements inside one another when each check depends on the previous condition.

public class Main {
    public static void main(String[] args) {
        int age = 25;
        boolean hasLicense = true;
        boolean hasVehicle = true;

        if (age >= 18) {
            if (hasLicense) {
                if (hasVehicle) {
                    System.out.println("Ready to drive.");
                }
            }
        }
    }
}
Output:
Ready to drive.

All three conditions must be true for the message to appear in this example.

7. Nested If with Else at Each Level

Each nested if statement can have its own else block.

public class Main {
    public static void main(String[] args) {
        int age = 17;
        boolean hasID = true;

        if (age >= 18) {
            if (hasID) {
                System.out.println("Entry allowed.");
            } else {
                System.out.println("ID required.");
            }
        } else {
            System.out.println("Age requirement not met.");
        }
    }
}
Output:
Age requirement not met.

Because the outer condition is false, Java skips the entire inner if-else block.

8. Nested If to Find the Largest of Three Numbers

Nested if statements can compare three numbers to determine the largest value.

public class Main {
    public static void main(String[] args) {
        int a = 25;
        int b = 40;
        int c = 30;

        if (a > b) {
            if (a > c) {
                System.out.println("A is largest");
            } else {
                System.out.println("C is largest");
            }
        } else {
            if (b > c) {
                System.out.println("B is largest");
            } else {
                System.out.println("C is largest");
            }
        }
    }
}
Output:
B is largest
This example assumes the numbers are not tied for the maximum. Equal values need additional checks if you want to report ties accurately.
9. Nested If for Login Validation

A nested if statement can represent a simple username-and-password check.

public class Main {
    public static void main(String[] args) {
        String username = "admin";
        String password = "12345";

        if (username.equals("admin")) {
            if (password.equals("12345")) {
                System.out.println("Login successful.");
            } else {
                System.out.println("Incorrect password.");
            }
        } else {
            System.out.println("Incorrect username.");
        }
    }
}
Output:
Login successful.

This is only a learning example. Real applications should use secure authentication and hashed passwords, not hard-coded credentials.

10. Nested If vs Logical AND

If both conditions must be true, you can sometimes replace nested if statements with the logical AND operator &&.

Using nested if:
if (age >= 18) {
    if (hasID) {
        System.out.println("Entry allowed.");
    }
}
Using logical AND:
if (age >= 18 && hasID) {
    System.out.println("Entry allowed.");
}

Both versions produce the same result for these conditions. Nested if statements are especially useful when the inner decision should be made only after the outer condition is satisfied.

11. Nested If with String Comparison

Strings can be compared using equals() inside nested conditions.

public class Main {
    public static void main(String[] args) {
        String course = "Java";
        int duration = 6;

        if (course.equals("Java")) {
            if (duration >= 6) {
                System.out.println("Eligible for the advanced course.");
            } else {
                System.out.println("Complete the required duration.");
            }
        } else {
            System.out.println("Course not available.");
        }
    }
}
Output:
Eligible for the advanced course.

Use equals() to compare String contents rather than using ==.

12. Common Nested If Mistakes
Mistake 1: Incorrect braces
if (age >= 18) {
    if (hasID) {
        System.out.println("Allowed");
    }
}

Make sure each opening brace has a matching closing brace.

Mistake 2: Using = instead of ==
// Correct comparison
if (number == 10) {
    System.out.println("Number is 10");
}
Mistake 3: Incorrect String comparison
// Correct way
if (username.equals("admin")) {
    System.out.println("Username matched");
}
Mistake 4: Forgetting the outer condition

The inner block will not execute if the outer condition is false, even when the inner condition would otherwise be true.

Keep nested conditions readable by using indentation and meaningful variable names.
13. Practical Example: Student Admission Eligibility

This example checks a student's age, marks, and entrance-test status before displaying an admission decision.

public class Main {
    public static void main(String[] args) {
        int age = 19;
        int marks = 75;
        boolean passedTest = true;

        if (age >= 18) {
            if (marks >= 60) {
                if (passedTest) {
                    System.out.println("Admission approved.");
                } else {
                    System.out.println("Entrance test required.");
                }
            } else {
                System.out.println("Minimum marks not achieved.");
            }
        } else {
            System.out.println("Minimum age requirement not met.");
        }
    }
}
Output:
Admission approved.
Lesson Summary:
  • A nested if places one if statement inside another.
  • The inner condition is evaluated only when execution reaches it.
  • Each if statement can have its own else block.
  • Nested conditions are useful for step-by-step decisions.
  • Logical operators can simplify some nested conditions.
  • Use correct braces, indentation, and String comparisons.

🧠 Quick Quiz

Q1. What is a nested if statement?



Q2. When is the inner if block checked in this example?

if (age >= 18) {
    if (hasID) {
        System.out.println("Allowed");
    }
}


Q3. Which method is recommended for comparing String contents?