The Stacks
BIG IDEA 3: ALGORITHMS AND PROGRAMMING · TOPIC 3.7

3.7 Nested Conditionals

A conditional can live inside another conditional's block. The inner one is only reached if the outer path leads there — tracing them carefully is the entire skill.

What you need to know

  • Nested conditionals are conditional statements placed inside the block of another conditional.
  • The inner condition is only evaluated when the outer branch containing it executes. If the outer condition is false, the inner one never runs at all.
  • Nesting lets you express "if A, then check B" logic — multiple criteria that depend on one another.
  • Nested IF/ELSE inside ELSE branches builds a multi-way choice (like grade ranges A/B/C/D/F). Only one leaf branch ever executes.
  • Many nested conditionals can be rewritten using AND/OR, and vice versa. The exam asks which rewrite is equivalent. Check with test values.
  • Deep nesting is hard to read; matching the braces to the correct IF is where tracing errors happen. Indentation in the exam's code is reliable — use it.

Worked example

IF (isMember)
{
    IF (total > 50)
    {
        discount ← 20
    }
    ELSE
    {
        discount ← 10
    }
}
ELSE
{
    discount ← 0
}

Members with a total over 50 get 20; other members get 10; non-members get 0. An equivalent flat version: IF (isMember AND total > 50) → 20, ELSE IF (isMember) → 10, ELSE → 0. Same outputs for every input, so they're equivalent.

Exam tip: Trace nested conditionals from the outside in. Decide the outer condition, cross out the branch that doesn't run, and only then look at what's inside the surviving branch. Never evaluate an inner condition before you've confirmed its outer branch runs.

Going deeper

The nuance, edge cases, and connections that turn a 3 into a 5.

  • The rule for nested conditionals: an inner condition is only evaluated if execution reaches it. If the outer condition is false and the inner IF is inside the outer's true-block, the inner condition is never checked at all — not "checked and found false," simply never checked.
  • Nested IF/ELSE in ELSE branches builds a chain: check A; if not A, check B; if not B, check C; else D. Exactly one leaf runs. This is how grade ranges, tax brackets, and any multi-way categorization is expressed.
  • Order in a chain matters when conditions overlap. Checking score ≥ 70 before score ≥ 90 sends a 95 down the 70 branch. Put the most restrictive condition first, or make them mutually exclusive with ranges.
  • A nested IF inside another IF's true-block, with no ELSEs, is equivalent to a single IF with AND: IF (a) { IF (b) { X } } ≡ IF (a AND b) { X }. Adding ELSEs breaks this equivalence — then you need to think about all the paths.
  • For "which rewrite is equivalent" questions, the technique is to enumerate paths: what inputs reach each DISPLAY or assignment in the original? Then check the candidate produces the same result for each.
  • Deep nesting (three or more levels) is a readability problem the exam sometimes asks about — the fix is usually a compound condition or a chain.

Mistakes that cost points

  • Evaluating an inner condition that was never reached. If the outer branch didn't run, nothing inside it did. Cross it out entirely before looking at what's inside.
  • Mis-pairing an ELSE. In block pseudocode the braces make it unambiguous, but students still attach an ELSE to the wrong IF when tracing quickly. Match each ELSE to the IF whose closing brace immediately precedes it.
  • Assuming equivalence after a superficial check. Testing one input isn't proof. For equivalence questions, 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 displayed by the following code?
x ← 5
y ← 12
IF (x > 3)
{
    IF (y < 10)
    {
        DISPLAY("one")
    }
    ELSE
    {
        DISPLAY("two")
    }
}
ELSE
{
    DISPLAY("three")
}
  1. A one
  2. B two
  3. C three
  4. D two three
Show answer

Answer: B. x > 3 is true, so the outer IF branch runs. Inside, y < 10 is false, so the inner ELSE displays "two". The outer ELSE never runs.

Q2 Which of the following is equivalent to the code below?
IF (a > 0)
{
    IF (b > 0)
    {
        DISPLAY("yes")
    }
}
  1. A IF (a > 0 OR b > 0) { DISPLAY("yes") }
  2. B IF (a > 0 AND b > 0) { DISPLAY("yes") }
  3. C IF (NOT (a > 0)) { DISPLAY("yes") }
  4. D IF (a > 0) { DISPLAY("yes") }
Show answer

Answer: B. "yes" appears only when both a > 0 and b > 0 — that's AND.

Key vocabulary

Nested conditional
a conditional statement placed inside the block of another conditional