1. Basic syntax of a nested if
In real life it’s rare to make a decision based on a single criterion. You usually have to check several conditions at once, and they often depend on each other. For example, to get into a concert, it’s not enough to be an adult—sometimes you also need to be on the guest list. This is where the so‑called multi-level logic comes in, and nested if statements help us implement it.
Nested conditions are when one if lives inside another. Imagine a box inside a box: if the outer condition is met, only then is the inner one checked. It’s a powerful control-flow tool.
Let’s start simple:
if (conditionA)
{
// actions if conditionA is true
if (conditionB)
{
// actions if both conditions are true
}
}
if inside another
if
If this reminds you of a matryoshka—you’ve got it right! The inner if executes only when the outer one has passed.
Example: entry to a club
Let’s extend our sample app where the user previously entered name and age. Now we’ll add a second level of checking: to enter a private club, you must be older than 21 and have an invitation code.
System.out.print("Enter your name: ");
String name = console.nextLine();
System.out.print("Enter your age: ");
int age = console.nextInt();
if (age >= 21)
{
System.out.print("Enter the invitation code: ");
String code = console.nextLine();
if (code.equals("VIP2024"))
{
System.out.println("Welcome, " + name + ", to the VIP club!");
}
else
{
System.out.println("Sorry, invalid code. Access denied.");
}
}
else
{
System.out.println("Sorry, entry is 21+ only!");
}
Note: the inner if checks the code only if the age has already passed validation. This saves resources (and the bouncer’s nerves).
2. How many levels of nesting are acceptable?
Technically—as many as you want. In practice: the more nesting levels, the harder the code is to read and maintain. People usually try to keep it to 2–3 levels. If you end up with a “ladder” of 4+ nested if statements, consider simplifying the logic or extracting checks into methods.
3. Nested if and else: pitfalls and nuances
Beginner mistake: sloppy use of curly braces.
In Java, when using else, there’s a strict rule: without curly braces {}, only the first statement after if or else belongs to it. This is a common source of annoying bugs.
Example with a potential error:
if (age >= 21)
if (code.equals("VIP2024"))
System.out.println("Welcome!");
else
System.out.println("Invalid code.");
Many expect the else to belong to the age check, but in fact it belongs to the closest preceding if (the invitation code check). That’s why the messages may not appear as intended. It’s better to always use braces—even if there’s only one line inside!
To remove any ambiguity, the compiler sees this code like this:
if (age >= 21)
{
if (code.equals("VIP2024"))
System.out.println("Welcome!");
else
System.out.println("Invalid code.");
}
Important! Once again: if an if-else construct has no curly braces, the else attaches to the previous (closest) if.
4. Alternatives: else if in nested conditions
Sometimes inside the outer if you want not only to check an additional condition, but also to provide an alternative. For example, if the invitation code didn’t match, you could offer guest access or deny entry.
if (age >= 21)
{
System.out.print("Enter the invitation code: ");
String code = console.nextLine();
if (code.equals("VIP2024"))
{
System.out.println("Welcome to the club!");
}
else if (code.equals("GUEST"))
{
System.out.println("Guest mode: access only until 23:00.");
}
else
{
System.out.println("Unfortunately, access is not possible.");
}
}
else
{
System.out.println("Entry is 21+ only.");
}
Here, inside the outer branch we make a nested choice among three scenarios: VIP code, guest code, and denial.
5. Practice: a discount calculator with multi-level logic
Suppose the user gets a discount based on age and whether they have a club card.
- If the user’s age is greater than or equal to 60—the discount is 15%.
- If the age is lower but the user has a club card—the discount is 7%.
- In all other cases there is no discount.
How to implement this?
System.out.print("Enter your age: ");
int age = console.nextInt();
double discount = 0.0;
if (age >= 60)
{
discount = 0.15;
}
else
{
System.out.print("Do you have a club card (yes/no)? ");
String hasCard = console.nextLine();
if (hasCard.equals("yes"))
{
discount = 0.07;
}
}
System.out.println("Your discount: " + (discount * 100) + "%");
Here we used a nested if inside the else branch: if the age doesn’t qualify, only then do we ask about the card.
GO TO FULL VERSION