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.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
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
- Topic 3.6/3.7 Conditionals and Nested Conditionals: conditional (IF/ELSE) statements select which code runs based on a Boolean condition, and nested conditionals handle multiple decision paths.
A focused answer to AP CSP Topics 3.6 and 3.7, covering IF and IF/ELSE selection, the role of the Boolean condition, nested conditionals for multiple paths, tracing which branch runs, and writing decision logic in AP CSP pseudocode.
- Topic 3.10 Lists: a list is an ordered collection of elements accessed by index; AP CSP lists are 1-indexed and support traversal and modification with APPEND, INSERT and REMOVE.
A focused answer to AP CSP Topic 3.10, covering lists as ordered collections, 1-based indexing in AP CSP pseudocode, accessing elements, traversing with FOR EACH and REPEAT, list operations (APPEND, INSERT, REMOVE, LENGTH), and why lists scale data processing.
- Topic 3.9 Developing Algorithms: an algorithm is a finite set of instructions that accomplishes a task, built by combining sequencing, selection and iteration, and different algorithms can solve the same problem.
A focused answer to AP CSP Topic 3.9, covering what an algorithm is, the three building blocks (sequencing, selection, iteration), expressing algorithms in pseudocode and language, that different algorithms can solve the same problem, and standard list algorithms.
- Topic 3.3/3.5 Mathematical and Boolean Expressions: programs evaluate arithmetic (including MOD) and Boolean expressions using relational and logical operators (AND, OR, NOT) that produce true or false.
A focused answer to AP CSP Topics 3.3 and 3.5, covering arithmetic operators and the MOD operator, relational operators, the Boolean operators AND, OR and NOT with truth tables, evaluating compound conditions, and the common uses of MOD such as testing even or odd.
- Topic 3.11/3.17/3.18 Binary Search and Efficiency: binary search finds a value in a sorted list far faster than linear search, and algorithms are compared by efficiency, with some problems being unsolvable or only approximable.
A focused answer to AP CSP Topics 3.11, 3.17 and 3.18, covering linear versus binary search, why binary search needs a sorted list, reasonable versus unreasonable running time, polynomial versus exponential growth, heuristics, and undecidable problems.
Sources & how we know this
- AP Computer Science Principles Course and Exam Description β College Board (2025)