Skip to main content

Unit 3 · Topic 3.7

3.7 Nested Conditionals

A nested conditional is an IF inside another IF or ELSE. It lets a program make a decision that depends on an earlier decision. This topic covers how to read, trace and write nested conditionals, and why the order of the checks matters.

Key terms

  • nested conditional
  • branch
  • condition
  • tracing

What nesting means

A nested conditional is a conditional statement inside another conditional statement. The inner one is only checked if the program takes the path that leads to it.

Use nesting when one decision depends on another. A store might first ask "Is this customer a member?" and only then ask "Is the order big enough for free shipping?", with a different cutoff for each group:

IF (isMember) { IF (total ≥ 25) { shipping ← 0 } ELSE { shipping ← 3 } } ELSE { IF (total ≥ 50) { shipping ← 0 } ELSE { shipping ← 7 } } DISPLAY(shipping)

A member with a $30 order pays 0. A non-member with the same order pays 7, because the program never checks the member cutoff for them.

Chains of choices

When there are more than two outcomes, nest an IF inside each ELSE. The exam's pseudocode has no ELSE IF keyword, so a chain looks like this:

IF (score ≥ 90) { grade ← "A" } ELSE { IF (score ≥ 80) { grade ← "B" } ELSE { IF (score ≥ 70) { grade ← "C" } ELSE { grade ← "F" } } } DISPLAY(grade)

Exactly one grade is assigned. As soon as one condition is true, its block runs and every ELSE below it is skipped.

How to trace nested conditionals

Pick a specific input and follow only the path it takes:

  • Evaluate the outer condition first.
  • Go into the matching block (the IF block if true, the ELSE block if false) and ignore the other block completely.
  • Repeat for any condition inside that block.
  • Write down which assignments actually ran.

Order matters

In a chain, the first true condition wins. That's why the grade code checks 90 first. Inside the first ELSE, you already know the score is below 90, so score ≥ 80 really means "80 to 89."

If the checks are in the wrong order, some branches become unreachable. If a chain checks score ≥ 70 first, a score of 95 gets a C, and the A branch can never run, because any score of 90 or more already passed the first check. The third worked example shows this.

Worked examples

Try each one yourself first, then open the solution.

  1. Example 1

    Tracing a chain

    Using the grade code above, what is displayed when score is 80? When it's 69?

    Show the solution
    1. Step 1: score = 80: 80 ≥ 90 is false, so go to the ELSE. 80 ≥ 80 is true, so grade = "B". The rest is skipped.
    2. Step 2: score = 69: 69 ≥ 90 false; 69 ≥ 80 false; 69 ≥ 70 false; so the innermost ELSE runs and grade = "F".

    Answer: B for 80; F for 69.

  2. Example 2

    Tracing two levels

    Using the shipping code above, find the shipping cost for (a) a member with a $10 total and (b) a non-member with a $50 total.

    Show the solution
    1. Step 1: (a) isMember is true, so go into the first block. 10 ≥ 25 is false, so the inner ELSE runs: shipping = 3.
    2. Step 2: (b) isMember is false, so go to the outer ELSE. 50 ≥ 50 is true, so shipping = 0.

    Answer: (a) 3 (b) 0

  3. Example 3

    The unreachable branch

    A student writes this to assign grades. What is displayed for a score of 95, and what's wrong with the code?IF (score ≥ 70) { grade ← "C" } ELSE { IF (score ≥ 90) { grade ← "A" } ELSE { grade ← "F" } } DISPLAY(grade)

    Show the solution
    1. Step 1: 95 ≥ 70 is true, so grade = "C" and the ELSE block is skipped. It displays C.
    2. Step 2: The inner score ≥ 90 check is inside the ELSE, which only runs when the score is below 70. A score below 70 can never be 90 or more, so "A" can never be assigned.
    3. Step 3: Fix: check the highest cutoff first, as in the grade code above.

    Answer: It displays C. The A branch is unreachable because the conditions are in the wrong order; check 90 before 70.

Common mistakes

  • Checking an inner condition when the outer condition sent the program the other way. Follow only one path.
  • Putting broad conditions before narrow ones, which makes later branches unreachable.
  • Losing track of which ELSE goes with which IF. Use the braces and indentation to match them up.

On the exam

  • Expect to trace nested conditionals for a specific input, or to choose which input produces a given output. Trace each choice rather than reasoning in general terms.
  • Some questions ask which of several code segments behave the same for all inputs. Test values at every boundary.

Connected topics

Videos

  • AP CSP Topic 3.7 - Nested Conditionals - Explanations and 5 MCQs!

    Dr_WuWatch on YouTube (opens in a new tab)

  • Nested conditionals | Intro to CS - Python | Khan Academy

    Khan AcademyWatch on YouTube (opens in a new tab)

  • Python Nested Conditionals - AP Computer Science Principles

    Chris OzarkaWatch on YouTube (opens in a new tab)

  • AP CSP Units 4 and 5 REVIEW Lesson 6 Nested Conditionals and Loops

    Janelle WhalenWatch on YouTube (opens in a new tab)

  • AP CSP Topic 3.7 - Nested Conditionals - Speedrun! 29 MCQs

    Dr_WuWatch on YouTube (opens in a new tab)

Check yourself

4 questions on 3.7 Nested Conditionals. Pick an answer to see if you got it, and why.

Code
IF (age < 12)
{
    price ← 6
}
ELSE
{
    IF (age ≥ 65)
    {
        price ← 7
    }
    ELSE
    {
        IF (isMatinee)
        {
            price ← 9
        }
        ELSE
        {
            price ← 12
        }
    }
}
Question 1 of 4

What is the value of price when age is 12 and isMatinee is false?

Question 2 of 4

What is the value of price when age is 65 and isMatinee is true?

Question 3 of 4

For which two inputs is price set to 9? Select two answers.

Select two answers. 0 of 2 chosen

Question 4 of 4

The variables a and b hold Boolean values. Which code segment always displays the same thing as the following code segment? IF (a) { IF (b) { DISPLAY("yes") } }

0 of 4 answered