UNIT 2: SELECTION AND ITERATION · TOPIC 2.4
2.4 Nested if Statements
Conditionals inside conditionals build multi-way decisions. The skill is knowing which else belongs to which if.
What you need to know
- A nested if is an if statement inside another's block. The inner one runs only if the outer branch containing it runs.
- An else-if chain —
if / else if / else if / else— tests conditions in order and runs the first true branch only. Later conditions are never checked once one matches. - Order matters in an else-if chain. Testing
score >= 70beforescore >= 90means 95 gets the 70 branch. - A trailing
elsecatches everything not matched above. Without it, it's possible no branch runs. - The dangling else: with no braces, an else pairs with the nearest preceding if. Braces remove the ambiguity.
- Nested ifs can often be rewritten as compound conditions with
&&(next topic); the exam asks which rewrite is equivalent.
Worked example
int score = 85;
if (score >= 90)
{
grade = "A";
}
else if (score >= 80)
{
grade = "B"; // runs; the rest is skipped
}
else if (score >= 70)
{
grade = "C";
}
else
{
grade = "F";
}
Reverse the order (test 70 first) and 85 becomes a C. First true branch wins.
Exam tip: Trace else-if chains top to bottom and stop at the first true condition. For nested ifs with no braces, an else attaches to the closest if above it — draw the braces in yourself before tracing.
Going deeper
The nuance, edge cases, and connections that turn a 3 into a 5.
- An else-if chain is nested if/else written flat.
if (A) X else if (B) Y else Zis exactlyif (A) { X } else { if (B) { Y } else { Z } }. Tracing: check A; if false, check B; if false, do Z. Exactly one of X, Y, Z runs. - Order matters when conditions overlap. With ranges, either order them so the most specific comes first, or make them disjoint with compound conditions. An unreachable branch (a condition that can never be true because an earlier one caught it) is a classic "what's wrong with this code" answer.
- Nested if without else is a filter:
if (a) { if (b) { X } }runs X only when both hold. Equivalent toif (a && b) { X }. Adding an else to either if breaks the equivalence — then it matters which if the else belongs to. - Dangling else:
if (a) if (b) X; else Y;— the else belongs to the inner if (b), not the outer. So Y runs when a is true and b is false. When a is false, nothing runs. Braces fix the ambiguity and the exam expects you to know the rule when they're absent. - Trace nested conditionals by elimination: decide the outer condition, cross out the branch that doesn't run, and only then look inside the surviving branch.
- Deep nesting is a readability smell. The fix is usually compound conditions or a chain — and equivalence questions ask which flat version matches a nested one.
Mistakes that cost points
- Evaluating a later branch after an earlier one matched. First true condition wins; the rest are skipped.
- Attaching a dangling else to the outer if. Nearest unmatched if. Inner.
- Assuming a flat rewrite is equivalent without checking each path. Test an input for each branch of the original.
Practice questions
Written in the style of the real exam. Try each one before revealing the answer.
Q1 What is printed by the following code when
x = 15?
if (x > 5)
{
if (x > 20)
{
System.out.print("big");
}
else
{
System.out.print("medium");
}
}
else
{
System.out.print("small");
}Show answer
Answer: B. x > 5 is true (outer branch). Inside, x > 20 is false, so the inner else prints "medium". The outer else never runs.
Q2 Consider the following code.
if (n >= 10)
System.out.print("X");
else if (n >= 100)
System.out.print("Y");
else
System.out.print("Z");For which values of n is "Y" printed?Show answer
Answer: D. Any n ≥ 100 is also ≥ 10, so the first branch catches it. The "Y" branch is unreachable.
Key vocabulary
- Nested if
- an if statement inside the block of another if
- else-if chain
- a series of conditions tested in order; only the first true one runs
- Dangling else
- an else that pairs with the nearest unbraced if