AP CSP Conditionals If Else
AP CSP Conditionals IF / ELSE IF / ELSE: Complete Guide (2025‑2026)
Selection lets a program choose which block of code to execute based on a condition. AP pseudocode uses IF, ELSE IF, and ELSE. A critical rule: exactly one branch executes per IF/ELSE chain — once a TRUE condition is found, the rest are skipped. The AP exam frequently tests the order of conditions and what happens at boundary values.
Contents
IF/ELSE Structure
Exactly one branch runs per execution. The condition decides which. Without ELSE, the program does nothing if the condition is FALSE.
- Runs block only if condition is TRUE
- If FALSE: skips block, continues after
- No alternative action performed
- Valid and common in programs
- Example: only add bonus if score ≥ 90
- TRUE: runs IF block, skips ELSE
- FALSE: skips IF block, runs ELSE
- Exactly one block always runs
- Guarantees action regardless of condition
- Example: pass/fail, yes/no, on/off
ELSE IF Chains and Order
In a chain, conditions are checked top to bottom. As soon as one is TRUE, that block runs and all remaining conditions are skipped entirely — even if they would also be TRUE.
Code Trace Gauntlet
What displays when score = 80?
B (80>90) = FALSE → skip. (80≥80) = TRUE → display B, remaining conditions skipped. Note: > vs ≥ at the boundary. If score were 90, ONLY the first condition fires (90>90 is FALSE, so ELSE IF fires).
What displays when x = 15?
medium x=15: outer IF (15>10) TRUE → enter outer block. Inner IF (15>20) FALSE → ELSE runs → display ‘medium’. Outer ELSE is never reached.
Note: these are THREE separate IFs, not a chain. What displays?
medium small All three IFs are evaluated independently. (5>10)=FALSE → nothing. (5>3)=TRUE → display ‘medium’. (5>0)=TRUE → display ‘small’. Two outputs because two IFs fire.
A student expected score 95 to display ‘excellent’. What actually displays?
pass (95≥60)=TRUE → display ‘pass’, remaining conditions skipped. The ELSE IF for ≥90 is never reached because the more-permissive condition fired first. Conditions must be ordered from most-restrictive to least-restrictive.
Spot the Bug
Two separate IFs both fire because x=15 satisfies both conditions. Outputs: ‘over 10’ and ‘over 5’. Fix: make the second an ELSE IF so only one branch can execute.
A score of exactly 90 fails (90>90 is FALSE) and falls to ELSE IF (90>80 is TRUE), receiving a B. Exactly 80 also gets wrong result. Fix: use ≥ (greater-than-or-equal) at both cutoffs.
Common Exam Pitfalls
Two consecutive IF statements both evaluate independently. ELSE IF creates a chain where only one can fire. This is the most common AP CSP conditional error.
Score 90 with condition score > 90 = FALSE. Score 90 with condition score ≥ 90 = TRUE. Always test exactly at the boundary value.
If you check ≥60 before ≥90, a score of 95 will match ≥60 and never reach the ≥90 branch. Order from most restrictive (highest/tightest) to least.
An IF without ELSE does nothing when the condition is FALSE. This is valid — no error — but means no output/action occurs for that case.
Check for Understanding
1. x ← 7
IF x > 10
DISPLAY("big")
ELSE IF x > 5
DISPLAY("medium")
ELSE
DISPLAY("small")
What is displayed?
- big
- medium
- small
- medium and small
2. Which value of score would cause the ELSE branch to run?IF score ≥ 90
DISPLAY("A")
ELSE IF score ≥ 80
DISPLAY("B")
ELSE
DISPLAY("F")
- 90
- 80
- 79
- 85
3. A student expects that a score of exactly 90 displays “A”. Their code has the condition score > 90. What actually happens?
- Displays A because 90 is close enough.
- Displays the next ELSE IF branch because 90 > 90 is FALSE.
- Causes a runtime error.
- Displays nothing because no ELSE is defined.
4. Consider:n ← 12
IF n > 5
DISPLAY("A")
IF n > 10
DISPLAY("B")
IF n > 15
DISPLAY("C")
What is displayed?
- A
- A and B
- A, B, and C
- B
5. Consider statements about IF/ELSE chains:
I. In an IF/ELSE IF/ELSE chain, at most one branch executes per run.
II. An IF statement without ELSE causes an error if the condition is FALSE.
III. Conditions in an ELSE IF chain are checked in order from top to bottom.
Which are correct?
- I only
- I and III only
- II and III only
- I, II, and III
6. grade ← 85
IF grade ≥ 60
DISPLAY("pass")
ELSE IF grade ≥ 80
DISPLAY("good")
ELSE IF grade ≥ 90
DISPLAY("excellent")
What is wrong with this code?
- The ELSE IF conditions use ≥ instead of >.
- The conditions are in wrong order — pass fires for all passing grades, good and excellent are unreachable.
- An ELSE block is required at the end.
- The code is correct and will display ‘good’ for grade 85.
7. What is the output when x = 5?IF x > 0
DISPLAY("pos")
IF x > 3
DISPLAY("big")
ELSE
DISPLAY("small")
- pos
- pos and big
- pos and small
- big
8. A temperature classifier: below 32 = ‘freeze’, 32-99 = ‘normal’, 100+ = ‘boil’. Which code is correct?
IF temp < 32: freeze
ELSE IF temp ≥ 32 AND temp < 100: normal
ELSE: boilIF temp < 32: freeze
IF temp < 100: normal
ELSE: boilIF temp ≥ 100: boil
ELSE IF temp ≥ 32: normal
ELSE IF temp < 32: freezeIF temp < 32: freeze
ELSE IF temp < 100: normal
ELSE: boil
9. After running the following with n = 3, what is displayed?IF n = 1
DISPLAY("one")
ELSE IF n = 2
DISPLAY("two")
ELSE IF n = 3
DISPLAY("three")
ELSE
DISPLAY("other")
- one
- three
- other
- two and three
10. Which change would make the following code display both “positive” and “large” when x = 50?IF x > 0
DISPLAY("positive")
ELSE IF x > 10
DISPLAY("large")
- Change ELSE IF to a separate IF statement.
- Change the condition to x > 0 AND x > 10.
- Swap the order of the two conditions.
- Add an ELSE block after the ELSE IF.
How the AP Exam Tests This
- Trace through an IF/ELSE IF/ELSE chain and identify which branch runs for a given input
- Identify the boundary value that causes a condition switch (e.g., when does ELSE IF fire instead of IF)
- Spot the error: conditions in wrong order causing unreachable branches
- Identify whether two separate IFs or an ELSE IF chain is used and what the difference produces
- I/II/III: which statements about selection are correct
FAQ
Get in Touch
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.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
tanner@apcsexamprep.com
Courses
AP CSA, CSP, & Cybersecurity
Response Time
Within 24 hours
Prefer email? Reach me directly at tanner@apcsexamprep.com