AP CSP Day 54: Complex Nested Conditionals
Share
Big Idea 3
Day 54 Practice
Focus: Complex Nested Conditionals
Practice Question
What does this code display?
x ← 7
y ← 10
IF (x > 5)
{
IF (y > 5)
{
DISPLAY("A")
}
ELSE
{
DISPLAY("B")
}
}
ELSE
{
DISPLAY("C")
}Why This Answer?
x > 5 is true (7 > 5), so we enter outer IF. Then y > 5 is true (10 > 5), so we display A.
Why Not the Others?
B) B displays only if x > 5 AND y ≤ 5.
C) C displays only if x ≤ 5.
D) One of the branches always executes.
Common Mistake
Watch Out!
Getting lost in nested IF statements. Trace through outer condition first, then inner conditions.
AP Exam Tip
For nested conditionals: evaluate outer condition first, then trace through only the branch that executes.