Trace if/elif/else chains to determine which branch executes
Explain why exactly one branch runs in an if/elif/else structure
Identify when condition order causes incorrect branching
Convert nested conditionals between Python and AP pseudocode
📈
Exam Impact: Nested conditionals appear in BI3 MCQ algorithm-tracing questions and in Create Task code logic.
💡 Why This Matters
Letter grades need five branches: A, B, C, D, F. A single if/else can only split two ways. Nested conditionals give you as many branches as you need while ensuring exactly one runs.
elif Chains
Python's elif chains multiple conditions. The FIRST true condition runs and all others are skipped.
grade <- 82
IF grade >= 90
{ DISPLAY("A") }
ELSE
{
IF grade >= 80
{ DISPLAY("B") }
ELSE
{ DISPLAY("C or lower") }
}
Key Rules
Rule 1: Exactly one branch executes. First true condition wins. Rule 2: Order matters. Put the most restrictive condition first. Rule 3: The final else is the catch-all.
Exam trap: If you check elif x >= 70 before elif x >= 80, a score of 85 hits the 70 branch and never reaches the 80 branch.
Practice Problems
🔎 Practice MCQ
Given x = 75, what does this code print?
if x >= 90:
print("A")
elif x >= 80:
print("B")
elif x >= 70:
print("C")
else:
print("F")
⚠️ Predict your answer BEFORE clicking.
🔎 Practice MCQ
Conditions written in order: if x >= 60, elif x >= 80, elif x >= 90. For x=95, which branch runs?
⚠️ Predict your answer BEFORE clicking.
🔎 Practice MCQ
Which code correctly categorizes age=15 as 'teen', age=30 as 'adult', age=8 as 'child'? I. if age>=18: ... elif age>=13: ... else: ... II. if age<13: ... elif age<18: ... else: ... III. if age>=13 and age<18: ... elif age>=18: ... else: ...
⚠️ Predict your answer BEFORE clicking.
🎮 Game
Nested Conditional Tracer
Trace multi-branch conditionals. Pick the correct output. 8 questions.
0
Correct
1/8
Question
0
Streak 🔥
🐛 Trace this code:
0/8
correct
💻 Python Code Editor
Practice Problems
Write Python and check your answer. Paste into Replit for complex programs.
Print negative/zero/positive for n=-3. Expected: negative
Hint: if n<0: print('negative') elif n==0: print('zero') else: print('positive')
Problem 3 of 3
Spot the bug: should print B for grade=85 but prints C. if grade>=70: print('C') elif grade>=80: print('B') elif grade>=90: print('A')
Hint: Wrong order. 85>=70 fires first. Fix: check >=90 first, then >=80, then >=70.
Frequently Asked Questions
Yes. Any nesting depth is valid. Be careful: deeply nested code becomes hard to read. More than 3 levels of nesting usually signals a logic simplification opportunity.
elif is part of the same chain: only one branch runs. A second if is independent and always evaluates its condition. Two separate ifs can both run; an if/elif chain guarantees exactly one branch.
Start at the top. Evaluate the first condition. If true, note which block runs and stop -- skip all elif/else. If false, move to the next elif. Continue until you find a true condition or reach else.
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed.
Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Typically responds within 24 hours
✓
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.