AP CSP Repeat N Times Loops
AP CSP REPEAT N TIMES Loops: Complete Guide (2025‑2026)
REPEAT N TIMES executes a block of code exactly N times — no more, no less. It is the simplest loop in AP pseudocode: the count is fixed before the loop starts. The body always executes N times regardless of anything that happens inside. AP CSP exam questions involving this loop almost always require you to trace the value of an accumulator variable through each iteration.
Contents
Structure and Behavior
REPEAT N TIMES is a counting loop. The body executes N times. Variables modified inside the body carry their updated values into the next iteration.
- Body executes exactly N times
- Count determined before loop starts
- No condition to check — count drives it
- Variables inside carry forward between iterations
- Use when: you know exactly how many times
- Count depends on data size → use FOR EACH
- Loop continues until condition met → REPEAT UNTIL
- Processing each element in a list → FOR EACH
- Count unknown in advance → REPEAT UNTIL
- Iterating with an index variable → see FOR EACH
Code Trace Gauntlet
What displays after the loop completes?
15 Start: total=0. Each iteration adds 3. After 5 iterations: 0+3+3+3+3+3 = 15.
What does DISPLAY output?
16 result=1 → 1*2=2 → 2*2=4 → 4*2=8 → 8*2=16. Each iteration doubles. 2^4 = 16.
What is count after the loop?
6 count starts at 0 and increments by 1 each of 6 iterations: 0,1,2,3,4,5,6. Final value: 6.
DISPLAY is inside the loop. What are all three outputs?
7 4 1 Iteration 1: x=10-3=7, display 7. Iteration 2: x=7-3=4, display 4. Iteration 3: x=4-3=1, display 1.
How many times does the innermost body execute?
6 Outer loop: 3 times. Inner loop: 2 times per outer iteration. Total: 3 x 2 = 6 executions. total = 6.
Spot the Bug
total starts at 1, not 0. After 5 iterations of +1: 1+1+1+1+1+1 = 6. The display shows 6, not 5. Fix: total ← 0. Accumulators almost always start at 0 for sums.
n=1 then adds 3 three times: 1+3+3+3 = 10. Wait — this is actually correct! But if the intent was doubling (1→2→4→8), the bug is using + instead of *. n ← n * 2 for doubling.
Common Exam Pitfalls
If N is 5, the body executes 5 times. Students frequently count incorrectly when tracing. Number each iteration explicitly.
Any variable you update inside the loop must be assigned a starting value before the loop. Uninitialized variables are errors.
REPEAT 0 TIMES is valid — the body simply never executes. The variable retains its pre-loop value.
REPEAT 5 TIMES with total ← total + 3 starting at 0 gives total = 15, not 5. The loop count determines how many times the update runs, not the final value.
Check for Understanding
1. n ← 0
REPEAT 4 TIMES
n ← n + 5
DISPLAY(n)
What is displayed?
- 5
- 16
- 20
- 4
2. How many times does the body of REPEAT 7 TIMES execute?
- 6
- 7
- 8
- Depends on the body
3. x ← 2
REPEAT 3 TIMES
x ← x * x
DISPLAY(x)
What is displayed?
- 8
- 512
- 256
- 16
3. x ← 2
REPEAT 3 TIMES
x ← x * x
DISPLAY(x)
What is displayed after all 3 iterations?
- 8
- 512
- 256
- 16
4. A student wants to print “hello” exactly 5 times. Which code is correct?
REPEAT 4 TIMES
DISPLAY("hello")REPEAT 5 TIMES
DISPLAY("hello")REPEAT 6 TIMES
DISPLAY("hello")x ← 0
REPEAT 5 TIMES
x ← x+1
DISPLAY("hello")
5. total ← 10
REPEAT 3 TIMES
total ← total - 4
DISPLAY(total)
What is displayed?
- -2
- 2
- -6
- 10
6. Consider:REPEAT 3 TIMES
REPEAT 3 TIMES
DISPLAY("*")
How many stars are displayed?
- 3
- 6
- 9
- 12
7. Consider statements about REPEAT N TIMES:
I. The loop body always executes exactly N times.
II. Variables modified inside the loop retain their values in subsequent iterations.
III. REPEAT 0 TIMES causes a runtime error.
Which are correct?
- I only
- I and II only
- I, II, and III
- II and III only
8. A program uses REPEAT N TIMES to sum the values 1+2+3+...+N. After the loop, the sum is incorrect — it is one value too high. The most likely cause is:
- N is assigned the wrong value.
- The accumulator was initialized to 1 instead of 0.
- The loop runs N+1 times.
- Addition inside a loop always produces off-by-one errors.
9. count ← 0
REPEAT 10 TIMES
count ← count + 2
DISPLAY(count)
What is displayed?
- 10
- 12
- 20
- 22
10. A REPEAT N TIMES loop is used to add 5 to a total N times. If total starts at 0 and the final total is 35, what is N?
- 5
- 6
- 7
- 8
How the AP Exam Tests This
- Trace an accumulator through N iterations and report its final value
- Identify the final value of a variable that doubles or multiplies through N iterations
- Determine N given the final accumulated value and the per-iteration change
- Nested REPEAT loops: count total body executions (outer count × inner count)
- Spot the initialization error (accumulator starts at wrong value)
FAQ
Get in Touch
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.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
tanner@apcsexamprep.com
Courses
AP CSA, CSP, & Cybersecurity
Response Time
Within 24 hours
Prefer email? Reach me directly at tanner@apcsexamprep.com