AP CSP Conditionals If Else

AP CSP Topics › Conditionals

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.

1Branch that executes per IF/ELSE chain — never more than one
0Times ELSE runs when IF condition is TRUE
NConditions checked in ELSE IF chains — stops at first TRUE

IF/ELSE Structure

IF / ELSE IF / ELSE: Only One Branch Executes condition TRUE or FALSE? TRUE IF block runs ELSE block skipped FALSE ELSE block runs IF block skipped Program continues here — exactly one branch ran

Exactly one branch runs per execution. The condition decides which. Without ELSE, the program does nothing if the condition is FALSE.

IF without ELSE
Condition gating
  • 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
IF with ELSE
Binary choice
  • 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.

Grade Classifier — Order Matters
IF score 90 DISPLAY(“A”) ELSE IF score 80 DISPLAY(“B”) ELSE IF score 70 DISPLAY(“C”) ELSE DISPLAY(“F”)

Code Trace Gauntlet

Trace 1 — Boundary Value Test
score 80 IF score > 90 DISPLAY(“A”) ELSE IF score 80 DISPLAY(“B”) ELSE DISPLAY(“C”)

What displays when score = 80?

Output

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).

Trace 2 — Nested IF
x 15 IF x > 10 IF x > 20 DISPLAY(“big”) ELSE DISPLAY(“medium”) ELSE DISPLAY(“small”)

What displays when x = 15?

Output

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.

Trace 3 — No ELSE Trap
n 5 IF n > 10 DISPLAY(“big”) IF n > 3 DISPLAY(“medium”) IF n > 0 DISPLAY(“small”)

Note: these are THREE separate IFs, not a chain. What displays?

Output

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.

Trace 4 — ELSE IF Order Bug
val 95 IF val 60 DISPLAY(“pass”) ELSE IF val 90 DISPLAY(“excellent”)

A student expected score 95 to display ‘excellent’. What actually displays?

Output

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

SPOT THE BUG — Missing ELSE — Falls Through to Wrong Output
# Intended: only one message per run x 15 IF x > 10 DISPLAY(“over 10”) IF x > 5 DISPLAY(“over 5”)
Bug Explained

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.

SPOT THE BUG — Wrong Operator at Boundary
# Intended: ≥90 = A, ≥80 = B IF score > 90 DISPLAY(“A”) ELSE IF score > 80 DISPLAY(“B”)
Bug Explained

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

1
Treating separate IFs as ELSE IF chains

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.

2
Wrong operator at boundary (> vs ≥)

Score 90 with condition score > 90 = FALSE. Score 90 with condition score ≥ 90 = TRUE. Always test exactly at the boundary value.

3
ELSE IF order: most restrictive first

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.

4
Assuming ELSE always exists

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
(7>10)=FALSE, skip. (7>5)=TRUE, display ‘medium’, stop. ELSE is skipped.

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
ELSE runs when score < 80. Score 79: (79≥90)=FALSE, (79≥80)=FALSE → ELSE runs, displays F.

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.
90 > 90 is FALSE. The program falls to the next ELSE IF or ELSE branch. The correct condition should be score ≥ 90.

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
Three independent IFs. (12>5)=TRUE → A. (12>10)=TRUE → B. (12>15)=FALSE → nothing. Output: A then 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
Statement I is correct. Statement III is correct. Statement II is false — no error occurs; the IF block is simply skipped.

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.
(85≥60)=TRUE fires immediately, displaying ‘pass’. The good and excellent branches are never reached. Conditions should be ordered most restrictive first: ≥90, then ≥80, then ≥60.

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
First IF: (5>0)=TRUE → display ‘pos’. Second IF/ELSE: (5>3)=TRUE → display ‘big’, ELSE skipped. Two outputs: pos, 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: boil
  • IF temp < 32: freeze
    IF temp < 100: normal
    ELSE: boil
  • IF temp ≥ 100: boil
    ELSE IF temp ≥ 32: normal
    ELSE IF temp < 32: freeze
  • IF temp < 32: freeze
    ELSE IF temp < 100: normal
    ELSE: boil
Option A correctly uses ELSE IF with explicit range. Option D is also structurally correct (since ELSE IF already implies temp ≥ 32), but Option A is more explicit. Option B uses separate IFs incorrectly. Option C has the order wrong for typical reading.

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
Conditions evaluated top-down: (3=1)=FALSE, (3=2)=FALSE, (3=3)=TRUE → display ‘three’. ELSE is skipped.

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.
Making the second branch a separate IF (not ELSE IF) allows it to evaluate independently. With x=50, both conditions are TRUE and both blocks execute.

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

Can there be multiple ELSE IF branches?
Yes, any number of ELSE IF branches can appear between IF and ELSE. They are evaluated top-to-bottom; the first TRUE one executes and the rest are skipped.
Does ELSE always need to be at the end?
Yes — ELSE is the ‘catch-all’ that runs when no previous condition was TRUE. It must be last in the chain. ELSE cannot have a condition; if you need a condition, use ELSE IF.
How does the AP exam test nested conditionals?
Nested IFs appear inside the block of an outer IF. Trace the outer condition first — if FALSE, the entire inner block is skipped. If TRUE, then evaluate the inner condition. The exam usually nests only one level deep.

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.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

tanner@apcsexamprep.com

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at tanner@apcsexamprep.com