AP · Computer Science Principles · Iteration and Algorithms

REPEAT Loops

13 min readUpdated December 1, 2026FreeAP Computer Science Principles, Algorithms and Programming

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

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

  2. 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 UNTIL stops, 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

In the code above, what would be displayed with REPEAT 10 TIMES instead?
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.

  1. Check the condition.
  2. If it is true, the loop ends and the program moves past the closing brace.
  3. 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

In the code above, how many passes are there if the condition is n > 30?
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

How many passes does REPEAT UNTIL(x ≥ 10) make when x starts at 12?
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 }:

Checkkk > 10Body runs?
11falseyes
24falseyes
37falseyes
410falseyes
513trueno: 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

How many passes does the loop above make if k starts at 11?
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.

  1. Step 1: Fill in the trace table.

    Passtotal afterk after
    112
    234
    378
    41516
    Why

    Inside each pass, total uses the value of k before k doubles, because that line comes first.

  2. Step 2: Read the display.

    DISPLAY(total) is after the loop, so it runs once and shows 15.

    Why

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

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

    Why

    A counter that goes up by 1 each pass records the number of passes.

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

  3. Step 3: Trace it.

    The checks see 100, 110, 120, 130 and 140 (false), then 150 (true). That is 5 passes.

    Why

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

  1. Step 1: Test the first check.

    x is 1. Option C is true at once (1 < 5), so C displays nothing.

    Why

    The condition is checked before the first pass.

  2. Step 2: Test the end.

    After 5 is displayed, x becomes 6. A is true at 6, so it stops there. B stops before 5 is displayed. D displays 6 too.

    Why

    The 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)
  1. Step 1: Start the trace.

    The checks see 3, 6, 12 and 24, all false, so after 3 passes n is 24 and count is 3.

  2. 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?
  1. 5 > 2 is 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

  1. 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 c is true.
  • C) The body runs while c is true.
  • D) c is 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 c is false.
  • D forgets that c is checked again before every pass.
  1. 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.
  • DISPLAY is inside the loop, so it shows a value on every pass.
  1. Apply k starts at 2. How many times does the body of REPEAT 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.
  1. Exam-style What value does count hold 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 ≥ 40 is true at the first check, so the body never runs.
  1. Exam-style In each option, x is set first and the loop runs next. Which one is an infinite loop?
  • A) x ← 0 then REPEAT UNTIL(x ≥ 9) { x ← x + 2 }
  • B) x ← 0 then REPEAT UNTIL(x = 9) { x ← x + 3 }
  • C) x ← 0 then REPEAT UNTIL(x = 9) { x ← x + 2 }
  • D) x ← 10 then REPEAT 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 when x reaches 10.
  • B reaches 9 exactly: 0, 3, 6, 9.
  • D counts down from 10 and hits 9 after one pass.
  1. 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 i becomes 5, before 5 is added, so it displays 10.
  • REPEAT UNTIL(i > 5) adds 1 to 5 and displays 15.
  1. Apply What value does count hold 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 TIMES gives exactly n passes, so the work grows with n.
  • REPEAT UNTIL checks 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.

Keep learning

Explore AP Computer Science Principles tutoring →

View the full Computer Science Principles syllabus →

More in this unit

Algorithms and Programming

All Iteration and Algorithms notes

Part of Novark's AP Computer Science Principles notes