Infinite loop: Stopping condition never becomes true. Off-by-one: range(5) gives 0,1,2,3,4 -- 5 iterations ending at 4. REPEAT UNTIL: Runs while condition is FALSE, stops when TRUE -- opposite intuition from while.
AP exam trap: REPEAT UNTIL stops when the condition becomes TRUE. A while loop in Python runs while the condition is TRUE. These are opposite.
Practice Problems
🔎 Practice MCQ
How many times does this loop body execute?
i <- 1
REPEAT UNTIL i > 5
{
i <- i + 1
}
⚠️ Predict your answer BEFORE clicking.
🔎 Practice MCQ
After this code, what is total?
total <- 0
REPEAT 4 TIMES
{
total <- total + 3
}
⚠️ Predict your answer BEFORE clicking.
🔎 Practice MCQ
A student loops over a 5-element list (indices 0-4). Which versions have an off-by-one error? I. for i in range(5) II. for i in range(1,5) III. for i in range(6)
⚠️ Predict your answer BEFORE clicking.
🎮 Game
Loop Tracer
Trace variable values through loops. 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.
Problem 1 of 3
Print the sum of 1 through 10 using a loop. Expected: 55
Hint: total = total + i inside the loop.
Problem 2 of 3
Print numbers 1 through 5 each on their own line.
Hint: for i in range(1, 6): print(i)
Problem 3 of 3
Spot the infinite loop: should count 1-5 but never stops. i = 1 while i <= 5: print(i)
Hint: Add i = i + 1 inside the loop. Without incrementing, the condition never becomes false.
Frequently Asked Questions
REPEAT N TIMES executes a fixed predetermined number of times. REPEAT UNTIL executes until a condition becomes true -- count unknown in advance. Use REPEAT N TIMES when you know the count; REPEAT UNTIL when waiting for a state change.
Trace the first and last iterations explicitly. For range(n), the last value is n-1. Always ask: what is the value on the first iteration? What triggers the stop?
No. AP pseudocode REPEAT UNTIL is a do-while: the body always executes at least once before the condition is checked. Python's while loop can execute zero times if the condition starts false.
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.