AP CSP Repeat Until Loops

AP CSP Topics › REPEAT UNTIL

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.

1+Minimum executions of the loop body — at least once, always
TRUECondition value that EXITS the loop (the opposite of what most expect)
Iterations if condition never becomes TRUE: infinite loop

Structure and the Post-Check Key

REPEAT UNTIL flow: condition checked AFTER body runs Loop Body condition TRUE or FALSE? FALSE loop again TRUE EXIT Body ALWAYS runs at least once. Loop stops when condition becomes TRUE.

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.

Scenario — Spot the Key Difference

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?

Answer

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

REPEAT N TIMES
Use when count is known
  • 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
REPEAT UNTIL condition
Use when stopping point is a condition
  • 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 1 — Basic Count-Up
x 1 REPEAT UNTIL x > 5 x x + 2 DISPLAY(x)

Trace x through each iteration. What is x when the loop exits?

Output

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

Trace 2 — Condition Already True at Start
n 20 REPEAT UNTIL n > 10 n n - 5 DISPLAY(n)

n starts at 20, which already satisfies n>10. How many times does the body run?

Output

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.

Trace 3 — Count Iterations
count 0 total 0 REPEAT UNTIL total 10 total total + 3 count count + 1 DISPLAY(count) DISPLAY(total)

How many iterations run? What are count and total?

Output

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.

Trace 4 — Stop Condition Direction
x 10 REPEAT UNTIL x = 0 x x - 3 DISPLAY(x)

Does this loop ever stop? What does it display?

Output

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

SPOT THE BUG — Condition Backwards — Loop Never Runs
# Intended: keep looping while x is small x 1 REPEAT UNTIL x < 100 x x * 2
Bug Explained

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.

SPOT THE BUG — Infinite Loop — Counter Not Updated
count 0 REPEAT UNTIL count = 5 DISPLAY(“hello”)
Bug Explained

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

1
REPEAT UNTIL exits when condition is TRUE, not FALSE

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

2
Body always runs at least once

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.

3
Infinite loops when condition skips over the target value

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.

4
Confusing REPEAT UNTIL with while loops in other languages

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
x=1. Iter 1: x=2. 2>4? F. Iter 2: x=3. 3>4? F. Iter 3: x=4. 4>4? F. Iter 4: x=5. 5>4? T → EXIT. Display: 5.

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
x=10 already satisfies x>5. But body executes FIRST: x=9. Then check: 9>5? TRUE → EXIT. Body ran exactly once.

3. Which value of x causes the loop to run exactly 3 times?

REPEAT UNTIL x = 0
x ← x - 1

  • 0
  • 1
  • 2
  • 3
Starting at 3: iter 1 x=2 (not 0), iter 2 x=1 (not 0), iter 3 x=0 (0=0 TRUE) → EXIT. 3 iterations.

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
I is correct — post-check means at least one execution. II is correct — TRUE exits the loop. III is false — they are different structures. REPEAT UNTIL stops at a condition; REPEAT N TIMES stops at a count.

5. total ← 0
REPEAT UNTIL total ≥ 20
total ← total + 7
DISPLAY(total)


What is displayed?

  • 20
  • 21
  • 14
  • 28
total=0. +7=7 (7≥20? F). +7=14 (14≥20? F). +7=21 (21≥20? T) → EXIT. Display: 21.

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 + 1
  • n ← 1
    REPEAT UNTIL n > 10
    DISPLAY(n)
    n ← n + 1
  • n ← 0
    REPEAT UNTIL n = 10
    n ← n + 1
    DISPLAY(n)
  • n ← 1
    REPEAT UNTIL n < 10
    DISPLAY(n)
    n ← n + 1
Option B: displays 1 through 10 then exits when n becomes 11 (n>10 TRUE). Option A stops at n=10 so 10 is displayed but loop exits without showing it (check: n=10, 10=10 TRUE, exits before the DISPLAY for n=10 can run again).

7. x ← 1
REPEAT UNTIL x > 100
x ← x * 2


What is x after the loop?

  • 100
  • 64
  • 128
  • 256
x doubles: 1,2,4,8,16,32,64,128. After 128: 128>100? TRUE → EXIT. x=128.

8. Which code creates an infinite loop?

  • x ← 0
    REPEAT UNTIL x = 10
    x ← x + 1
  • x ← 1
    REPEAT UNTIL x > 10
    x ← x + 3
  • x ← 0
    REPEAT UNTIL x = 5
    x ← x + 2
  • x ← 10
    REPEAT UNTIL x < 0
    x ← x - 1
Option C: x goes 0,2,4,6,8,10... and skips over 5 (even numbers only). x=5 is never TRUE → infinite loop. Option B: x goes 1,4,7,10,13 — 13>10 TRUE eventually.

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
Java’s do-while executes the body first then checks the condition — exactly like REPEAT UNTIL. Python’s while checks the condition first.

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
DISPLAY is inside the loop after the update. Iter 1: n=3 display 3. Iter 2: n=1 display 1. Iter 3: n=-1 display -1. Check: -1<0 TRUE → EXIT. Outputs: 3, 1, -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

How do I decide between REPEAT UNTIL and REPEAT N TIMES?
Use REPEAT N TIMES when you know the exact count in advance. Use REPEAT UNTIL when you don’t know how many iterations are needed — you only know the stopping condition.
What is an infinite loop and how does it happen?
An infinite loop occurs when the REPEAT UNTIL condition never becomes TRUE. Common causes: forgetting to update the variable the condition checks, using = when the value skips over the target, or having the condition work in the wrong direction.

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

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]