AP CSP Repeat Until Loops
AP CSP REPEAT UNTIL Loops: Complete Guide (2025‑2026)
REPEAT UNTIL repeats the loop body until its condition becomes TRUE. Critical distinction from REPEAT N TIMES: the body always runs at least once because the condition is checked after the body executes. The loop stops when the condition is TRUE — it keeps going while the condition is FALSE. This is the opposite of how most students expect it to work.
Contents
Structure and the Post-Check Key
The condition is checked AFTER the body runs. This means the body always executes at least once. The loop exits when the condition is TRUE — it continues while FALSE.
A student expects REPEAT UNTIL x > 10 with x = 15 to skip the body entirely since 15 > 10 is already TRUE at the start.
What actually happens? How many times does the body run?
The body runs exactly once. In REPEAT UNTIL, the body executes FIRST, then the condition is checked. Even though x = 15 already satisfies the condition before the loop starts, the body still runs once before the check occurs. This is the #1 REPEAT UNTIL misconception on the AP exam.
REPEAT UNTIL vs. REPEAT N TIMES
- Runs body exactly N times
- Count known before loop starts
- Condition never checked — count drives it
- Body may run 0 times (REPEAT 0 TIMES)
- Example: do something for each of N items
- Runs body until condition becomes TRUE
- Count unknown in advance
- Body ALWAYS runs at least once (post-check)
- Risk of infinite loop if condition never TRUE
- Example: keep asking for input until valid
Code Trace Gauntlet
Trace x through each iteration. What is x when the loop exits?
7 Iteration 1: x=1, body: x=3. Check: 3>5? FALSE → continue. Iteration 2: x=3, body: x=5. Check: 5>5? FALSE → continue. Iteration 3: x=5, body: x=7. Check: 7>5? TRUE → EXIT. DISPLAY: 7
n starts at 20, which already satisfies n>10. How many times does the body run?
15 Body runs ONCE before condition checked. After body: n=20-5=15. Check: 15>10? TRUE → EXIT. Display: 15. Common trap: students expect 0 iterations, but REPEAT UNTIL always runs at least once.
How many iterations run? What are count and total?
4 12 Iter 1: total=3, count=1. Check: 3≥10? F. Iter 2: total=6, count=2. Check: 6≥10? F. Iter 3: total=9, count=3. Check: 9≥10? F. Iter 4: total=12, count=4. Check: 12≥10? T → EXIT. count=4, total=12.
Does this loop ever stop? What does it display?
This is an INFINITE LOOP. Iter 1: x=7. Check: 7=0? F. Iter 2: x=4. Check: 4=0? F. Iter 3: x=1. Check: 1=0? F. Iter 4: x=-2. Check: -2=0? F. x skips over 0 (goes 10,7,4,1,-2,-5...) and NEVER equals exactly 0. The condition is never TRUE → infinite loop.
Spot the Bug
REPEAT UNTIL exits when condition is TRUE. x=1 < 100 is TRUE immediately after the first iteration... wait, actually the body runs once: x=2. Check: 2<100? TRUE → EXIT. But the intended behavior is to loop until x reaches 100+. Fix: REPEAT UNTIL x ≥ 100.
count is never changed inside the loop body. count stays 0 forever. 0=5 is always FALSE → infinite loop. Fix: add count ← count + 1 inside the loop body.
Common Exam Pitfalls
The loop continues while condition is FALSE and stops when TRUE. This is opposite of what many students expect. Remember: UNTIL means ‘run until this becomes true.’
Condition is checked after the body. Even if the condition is already TRUE before the loop starts, the body runs once. This is the post-check behavior.
If the step size causes x to jump past the exit value (e.g., adding 3 when the condition checks for x = 10, starting at 1), the condition may never be TRUE.
Python’s while loop runs WHILE the condition is TRUE. AP’s REPEAT UNTIL runs UNTIL the condition is TRUE (i.e., while it is FALSE). Opposite stop condition logic.
Check for Understanding
1. x ← 1
REPEAT UNTIL x > 4
x ← x + 1
DISPLAY(x)
What is displayed?
- 4
- 5
- 6
- 1
2. x ← 10
REPEAT UNTIL x > 5
x ← x - 1
How many times does the loop body execute?
- 0 — the condition is already true before the loop starts
- 1 — body runs once then condition checked and found TRUE
- 5 — loops until x reaches 5
- 10 — loops until x reaches 0
3. Which value of x causes the loop to run exactly 3 times?REPEAT UNTIL x = 0
x ← x - 1
- 0
- 1
- 2
- 3
4. Consider statements about REPEAT UNTIL:
I. The loop body always executes at least once.
II. The loop exits when the condition evaluates to TRUE.
III. REPEAT UNTIL is equivalent to REPEAT N TIMES when N is unknown.
Which are correct?
- I only
- I and II only
- II and III only
- I, II, and III
5. total ← 0
REPEAT UNTIL total ≥ 20
total ← total + 7
DISPLAY(total)
What is displayed?
- 20
- 21
- 14
- 28
6. A student writes a loop to count from 1 to 10. Which version correctly uses REPEAT UNTIL?
n ← 1
REPEAT UNTIL n = 10
DISPLAY(n)
n ← n + 1n ← 1
REPEAT UNTIL n > 10
DISPLAY(n)
n ← n + 1n ← 0
REPEAT UNTIL n = 10
n ← n + 1
DISPLAY(n)n ← 1
REPEAT UNTIL n < 10
DISPLAY(n)
n ← n + 1
7. x ← 1
REPEAT UNTIL x > 100
x ← x * 2
What is x after the loop?
- 100
- 64
- 128
- 256
8. Which code creates an infinite loop?
x ← 0
REPEAT UNTIL x = 10
x ← x + 1x ← 1
REPEAT UNTIL x > 10
x ← x + 3x ← 0
REPEAT UNTIL x = 5
x ← x + 2x ← 10
REPEAT UNTIL x < 0
x ← x - 1
9. REPEAT UNTIL in AP pseudocode is most similar to which Java/Python construct?
- Java for loop
- Python while loop
- Java do-while loop
- Python for-in loop
10. n ← 5
REPEAT UNTIL n < 0
n ← n - 2
DISPLAY(n)
What values are displayed?
- 5, 3, 1
- 3, 1, -1
- 5, 3, 1, -1
- 3, 1
How the AP Exam Tests This
- Trace REPEAT UNTIL with a starting value and identify the value of a variable when the loop exits
- Determine how many iterations a REPEAT UNTIL loop executes for a given starting value
- Identify infinite loop conditions (step size skips past target)
- I/II/III: which statements about REPEAT UNTIL are correct
- Compare REPEAT UNTIL and REPEAT N TIMES behavior for the same starting conditions
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.
Prefer email? Reach me directly at [email protected]