A REPEAT n TIMES loop runs its block exactly n times. A REPEAT UNTIL(condition) loop checks its condition before every pass, runs the block while the condition is false and stops as soon as it is true, so it can run zero times or never stop. In AP® Computer Science Principles, you trace either kind of loop with a trace table, one row per pass, and count how many times the body runs.
Quick check
x holds 4. What does x hold after IF(x > 3) { x ← x + 1 } runs?Show answerHide answer
It holds 5. The condition is true, so the block runs once.
How many times is hi displayed by REPEAT 3 TIMES { DISPLAY("hi") }?Show answerHide answer
Three times: the block runs once per pass, and there are 3 passes.
Know these already? Jump to the practice questions
What you'll be able to do
- Determine how many times a loop body runs.
- Predict what a loop displays, using a trace table.
- Explain when
REPEAT UNTILstops, including loops that never run and loops that never stop. - Choose a stopping condition that makes a loop do exactly what is asked.
Key terms
Iteration
Repeating a block of statements, either a set number of times or until a condition is met.
- In plain English
- Doing the same steps again and again.
- Don't write
- "Iteration chooses between two blocks."
Loop body
The block of statements inside a loop's braces; one run of it is a pass.
- In plain English
- The part that gets repeated.
- Don't write
- "The loop body runs once for each line it contains."
Infinite loop
A loop whose stopping condition never becomes true, so it never ends.
- In plain English
- A loop stuck going round forever.
- Don't write
- "An infinite loop is a loop that runs a large number of times."
Trace table
A table with one column per variable and one row per pass, recording the values as a loop runs.
- In plain English
- A written log of what each variable holds after each pass.
- Don't write
- "A trace table shows only the final values."
What does REPEAT n TIMES do?
On the exam reference sheet: REPEAT n TIMES { … }, which runs the block n times.
Use it when you know the number of passes before the loop starts. The count can be a number or a variable, and REPEAT 0 TIMES runs the body zero times.
total ← 0
REPEAT 4 TIMES
{
total ← total + 3
}
DISPLAY(total)
Each pass adds 3, so after 4 passes total is 12. The DISPLAY sits after the closing brace, so it runs once, after the loop, and shows 12.
The number of passes is n, so doubling n doubles the work the loop does.
Check yourself
Show answerHide answer
30: ten passes of adding 3.
When does REPEAT UNTIL stop?
On the exam reference sheet: REPEAT UNTIL(condition) { … }, which repeats the block until the condition is true.
Learn this: the condition is checked before each pass, including the first.
- Check the condition.
- If it is true, the loop ends and the program moves past the closing brace.
- If it is false, run the body once, then go back to step 1.
The body must change something the condition depends on, or every check gives the same answer.
n ← 1
REPEAT UNTIL(n > 20)
{
n ← n * 3
}
The checks see 1, 3, 9 and 27. The first three are false, so the body runs 3 times, and the loop ends with n holding 27.
Key idea
REPEAT UNTIL runs while its condition is false and stops at the first check where it is true.
Check yourself
Show answerHide answer
Four passes: the checks see 1, 3, 9 and 27 (all false, because 27 is not more than 30), then 81 (true).
When does a loop never run, or never stop?
A REPEAT UNTIL loop runs zero times when its condition is already true at the first check. With x holding 12, REPEAT UNTIL(x ≥ 10) skips its body entirely.
A loop never stops, an infinite loop, when the condition can never become true. Two common causes:
- The body never changes the variables in the condition.
- The variable jumps over the stopping value.
x ← 0
REPEAT UNTIL(x = 7)
{
x ← x + 2
}
x goes 0, 2, 4, 6, 8, 10 and so on. It is never exactly 7, so the loop never ends. REPEAT UNTIL(x ≥ 7) stops after 4 passes, with x holding 8.
Check yourself
Show answerHide answer
Zero. 12 ≥ 10 is true at the first check.
How do you count passes with a trace table?
List the value the condition sees at each check, until the first check that is true. The number of passes is the number of false checks before it.
For k ← 1 and REPEAT UNTIL(k > 10) { k ← k + 3 }:
| Check | k | k > 10 | Body runs? |
|---|---|---|---|
| 1 | 1 | false | yes |
| 2 | 4 | false | yes |
| 3 | 7 | false | yes |
| 4 | 10 | false | yes |
| 5 | 13 | true | no: loop ends |
So the body runs 4 times. Watch the boundary row: 10 is not more than 10, so that check is false. The note on developing and comparing algorithms builds on these counts.
Check yourself
Show answerHide answer
Zero: 11 > 10 is true at the first check.
Worked example 1: Trace a REPEAT n TIMES loop
Question: What is displayed when this code segment runs?
total ← 0
k ← 1
REPEAT 4 TIMES
{
total ← total + k
k ← k * 2
}
DISPLAY(total)
What tells you the method
A fixed number of passes with two changing variables, so trace with one row per pass.
Step 1: Fill in the trace table.
Pass totalafterkafter1 1 2 2 3 4 3 7 8 4 15 16 WhyInside each pass,
totaluses the value ofkbeforekdoubles, because that line comes first.Step 2: Read the display.
DISPLAY(total)is after the loop, so it runs once and shows 15.WhyOnly statements inside the braces repeat.
Check it
The loop adds 1 + 2 + 4 + 8 = 15.
Answer
15
Worked example 2: Design a savings counter
Question: A balance starts at 100 and grows by 10 each month. Design a code segment that displays the number of months until the balance is at least 150. What does it display?
What tells you the method
The number of passes is the unknown, so use REPEAT UNTIL with a counter.
Step 1: State the input, output and plan.
Input: the starting balance. Output: the number of months. Plan: add 10 and count one month per pass, until the balance reaches 150.
WhyA counter that goes up by 1 each pass records the number of passes.
Step 2: Write the code.
balance ← 100 months ← 0 REPEAT UNTIL(balance ≥ 150) { balance ← balance + 10 months ← months + 1 } DISPLAY(months)Why≥rather than=means the loop still stops if the balance jumps past 150.Step 3: Trace it.
The checks see 100, 110, 120, 130 and 140 (false), then 150 (true). That is 5 passes.
WhyEach false check is one pass.
Check it
At the boundary, a starting balance of 150 makes the first check true, so the loop never runs and the code displays 0, which is correct.
Answer
5
Worked example 3: Choose the stopping condition
Question: Which condition can replace <MISSING CONDITION> so that the code segment displays 1 2 3 4 5?
x ← 1
REPEAT UNTIL(<MISSING CONDITION>)
{
DISPLAY(x)
x ← x + 1
}
- A)
x > 5 - B)
x = 5 - C)
x < 5 - D)
x > 6
What tells you the method
A missing loop condition: test each option at the first check and at the last value, 5.
Step 1: Test the first check.
xis 1. Option C is true at once (1 < 5), so C displays nothing.WhyThe condition is checked before the first pass.
Step 2: Test the end.
After 5 is displayed,
xbecomes 6. A is true at 6, so it stops there. B stops before 5 is displayed. D displays 6 too.WhyThe last value displayed is the last one that passes the check.
Check it
With A, the checks see 1 to 5 (false) and then 6 (true): 5 passes, displaying 1 to 5.
Answer
A
Your turn
Question: What is displayed when this code segment runs?
n ← 3
count ← 0
REPEAT UNTIL(n > 100)
{
n ← n * 2
count ← count + 1
}
DISPLAY(count)
DISPLAY(n)
Step 1: Start the trace.
The checks see 3, 6, 12 and 24, all false, so after 3 passes
nis 24 andcountis 3.Step 2: Your go: continue the trace until the check is true, then write what is displayed.
Show the full solutionHide the solution
Step 2. Pass 4 gives 48, pass 5 gives 96 and pass 6 gives 192. 192 > 100 is true, so the loop ends with count holding 6.
Answer: 6 192
Traps
Trap
The body of a REPEAT UNTIL loop always runs at least once.
It seems right because the condition is about when to stop, so it feels like something must happen first. But the condition is checked before each pass, so a condition that starts true means zero passes.
count starts at 0 and x at 5. After REPEAT UNTIL(x > 2) { count ← count + 1 }, what does count hold?
5 > 2is true at the first check.
Trap
REPEAT UNTIL(x = 7) stops once x goes past 7.
It seems right because x has passed its target. But = is only true when x is exactly 7, so a step that jumps over 7 gives an infinite loop. Use ≥ when a step might skip the value.
x starts at 1 and the body is x ← x + 3. Does REPEAT UNTIL(x = 9) end?
No. x goes 1, 4, 7, 10, … and is never 9.
Trap
REPEAT UNTIL keeps going while its condition is true.
It seems right because many loops are described as "while something is true". But REPEAT UNTIL runs while its condition is false and ends when it becomes true.
Does REPEAT UNTIL(done) run its body when done is false?
Yes. It runs while done is false and stops once done is true.
What's examined
On the AP® Computer Science Principles exam, loops belong to Big Idea 3: Algorithms and Programming. Multiple-choice questions ask what a loop displays, how many times its body runs, which condition completes it, or which loop never ends. REPEAT n TIMES and REPEAT UNTIL are on the exam reference sheet; the check-before-each-pass rule is Learn this.
Practice questions
- Recall Which statement about
REPEAT UNTIL(c) { … }is true?
- A) The body always runs at least once.
- B) The loop ends at the first check where
cis true. - C) The body runs while
cis true. - D)
cis checked only once, before the first pass.
Show answer
Answer: B. The condition is checked before each pass, and a true check ends the loop.
Why the others are wrong:
- A ignores the check before the first pass.
- C reverses the rule: the body runs while
cis false. - D forgets that
cis checked again before every pass.
- Apply What is displayed when this code segment runs?
x ← 1
REPEAT 3 TIMES
{
x ← x * 2 + 1
DISPLAY(x)
}
Show answer
Answer: 3 7 15
Marking points:
- Pass 1: 1 × 2 + 1 = 3. Pass 2: 3 × 2 + 1 = 7. Pass 3: 7 × 2 + 1 = 15.
DISPLAYis inside the loop, so it shows a value on every pass.
- Apply
kstarts at 2. How many times does the body ofREPEAT UNTIL(k > 30) { k ← k + 5 }run?
Show answer
Answer: 6
Marking points:
- The checks see 2, 7, 12, 17, 22 and 27 (false), then 32 (true).
- Six false checks means six passes.
- Exam-style What value does
counthold after this code segment runs?
count ← 0
total ← 50
REPEAT UNTIL(total ≥ 40)
{
total ← total - 5
count ← count + 1
}
Show answer
Answer: 0
Marking points:
50 ≥ 40is true at the first check, so the body never runs.
- Exam-style In each option,
xis set first and the loop runs next. Which one is an infinite loop?
- A)
x ← 0thenREPEAT UNTIL(x ≥ 9) { x ← x + 2 } - B)
x ← 0thenREPEAT UNTIL(x = 9) { x ← x + 3 } - C)
x ← 0thenREPEAT UNTIL(x = 9) { x ← x + 2 } - D)
x ← 10thenREPEAT UNTIL(x = 9) { x ← x - 1 }
Show answer
Answer: C. x takes only even values, 0, 2, 4, …, so x = 9 is never true.
Why the others are wrong:
- A uses
≥, so it stops whenxreaches 10. - B reaches 9 exactly: 0, 3, 6, 9.
- D counts down from 10 and hits 9 after one pass.
- Exam-style This code segment should display the sum 1 + 2 + 3 + 4 + 5, which is 15. Give the number of the line that stops it working.
1: sum ← 0
2: i ← 1
3: REPEAT UNTIL(i = 5)
4: {
5: sum ← sum + i
6: i ← i + 1
7: }
8: DISPLAY(sum)
Show answer
Answer: Line 3.
Marking points:
- The loop stops when
ibecomes 5, before 5 is added, so it displays 10. REPEAT UNTIL(i > 5)adds 1 to 5 and displays 15.
- Apply What value does
counthold after this code segment runs?
count ← 0
REPEAT 3 TIMES
{
REPEAT 4 TIMES
{
count ← count + 1
}
}
Show answer
Answer: 12
Marking points:
- Each of the 3 outer passes runs the inner loop 4 times.
- 3 × 4 = 12 increases.
Key points
Recall, then reveal
Say the main points from memory first, then check them.
Show key pointsHide key points
REPEAT n TIMESgives exactly n passes, so the work grows with n.REPEAT UNTILchecks first, runs while false and stops at the first true check.- A condition that starts true means zero passes.
- A condition that can never become true means an infinite loop; prefer
≥to=when a step might jump over the target. - Count passes as the number of false checks, and test the boundary values.