Skip to main content
United StatesComputer Science PrinciplesSyllabus dot point

How do loops repeat instructions, and how do you avoid infinite loops and off-by-one errors?

Topic 3.8 Iteration: iteration (REPEAT n TIMES and REPEAT UNTIL) repeats a block of code, with the number of repetitions controlled by a count or a condition.

A focused answer to AP CSP Topic 3.8, covering REPEAT n TIMES and REPEAT UNTIL loops in AP CSP pseudocode, counting iterations, accumulating values, infinite loops and off-by-one errors, and tracing loop execution.

Generated by Claude Opus 4.810 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this topic is asking
  2. REPEAT n TIMES
  3. REPEAT UNTIL
  4. Counters and accumulators
  5. Infinite loops and off-by-one errors
  6. Try this

What this topic is asking

The College Board (Topic 3.8) wants you to use iteration: repeating a block of code. AP CSP pseudocode has two loop forms: REPEAT n TIMES (run the body a fixed number of times) and REPEAT UNTIL (condition) (run the body until a condition becomes true). You need to count iterations, accumulate values across passes, trace loop execution, and avoid the classic faults: infinite loops and off-by-one errors.

REPEAT n TIMES

REPEAT 3 TIMES
{
  DISPLAY("Hello")
}

This displays Hello three times.

REPEAT UNTIL

count ← 1
REPEAT UNTIL (count > 3)
{
  DISPLAY(count)
  count ← count + 1
}

This displays 1, 2, 3, then stops because count reaches 4.

Counters and accumulators

Loops commonly maintain:

  • A counter, a variable increased each pass to count iterations or generate a sequence (count ← count + 1).
  • An accumulator, a running total or result built up across passes (sum ← sum + value). Initialise it before the loop.

Infinite loops and off-by-one errors

A REPEAT UNTIL loop becomes infinite if the stopping condition can never become true, usually because the variable in the condition is never updated. Always change something inside the loop that moves toward the stopping condition. An off-by-one error runs the loop one time too many or too few, often from a boundary like > versus >=.

Try this

Q1. How many times does the body of REPEAT 6 TIMES { ... } run? [1 point]

  • Cue. Exactly 6 times.

Q2. What causes a REPEAT UNTIL loop to run forever, and how do you prevent it? [2 points]

  • Cue. It runs forever if the stopping condition never becomes true; prevent it by updating a variable inside the loop so the condition will eventually be met.

Exam-style practice questions

Practice questions written in the style of College Board exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.

AP 2022 (style)1 marksMultiple choice. What is the value of `sum` after this AP CSP pseudocode runs? ``` sum ← 0 count ← 1 REPEAT UNTIL (count > 4) { sum ← sum + count count ← count + 1 } ``` (A) 6 (B) 10 (C) 15 (D) The loop never ends.
Show worked answer β†’

The answer is (B).

Trace it. Start sum = 0, count = 1. The loop runs while count > 4 is false, that is while count <= 4. Iterations add count and increase it: add 1 (sum 1, count 2), add 2 (sum 3, count 3), add 3 (sum 6, count 4), add 4 (sum 10, count 5). Now count > 4 is true, so the loop stops with sum = 10. (A) stops one iteration early; (C) adds an extra value; (D) is wrong because count increases each time, so the condition eventually becomes true.

Markers reward tracing a REPEAT UNTIL loop, including the final check that the stopping condition is reached.

AP 2021 (style)3 marksFree response (code writing). Write AP CSP pseudocode using a loop that displays the numbers 1 through 5, each on its own line.
Show worked answer β†’

A 3-point question on counting iteration.

n ← 1
REPEAT 5 TIMES
{
  DISPLAY(n)
  n ← n + 1
}

Point 1: initialise n to 1 before the loop. Point 2: REPEAT 5 TIMES runs the body exactly five times. Point 3: DISPLAY(n) then increment n so each pass shows the next number, giving 1, 2, 3, 4, 5. A common error is forgetting to increment n, which would display 1 five times.

Related dot points

Sources & how we know this