AP CSP Repeat N Times Loops

AP CSP Topics › REPEAT N TIMES

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.

NTimes the loop body runs — exactly N, never N-1 or N+1
0Times body runs with REPEAT 0 TIMES
1Variable you almost always need to initialize before the loop: the accumulator

Structure and Behavior

REPEAT 4 TIMES: Iteration Count Trace Iteration 1 body executes Iteration 2 body executes Iteration 3 body executes Iteration 4 body executes DONE Variable State: total ← 0 then +5 each iteration Start: 0 After 1: 5 After 2: 10 After 3: 15 After 4: 20 REPEAT 4 TIMES with total ← total + 5: REPEAT N TIMES runs the body exactly N times, not N-1 or N+1

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.

REPEAT N TIMES
Fixed-count 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
When NOT to Use REPEAT N TIMES
Better alternatives exist when:
  • 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

Trace 1 — Basic Accumulator
total 0 REPEAT 5 TIMES total total + 3 DISPLAY(total)

What displays after the loop completes?

Output

15 Start: total=0. Each iteration adds 3. After 5 iterations: 0+3+3+3+3+3 = 15.

Trace 2 — Multiplication via Addition
result 1 REPEAT 4 TIMES result result * 2 DISPLAY(result)

What does DISPLAY output?

Output

16 result=1 → 1*2=2 → 2*2=4 → 4*2=8 → 8*2=16. Each iteration doubles. 2^4 = 16.

Trace 3 — Counter Inside Loop
count 0 REPEAT 6 TIMES count count + 1 DISPLAY(count)

What is count after the loop?

Output

6 count starts at 0 and increments by 1 each of 6 iterations: 0,1,2,3,4,5,6. Final value: 6.

Trace 4 — DISPLAY Inside the Loop
x 10 REPEAT 3 TIMES x x - 3 DISPLAY(x)

DISPLAY is inside the loop. What are all three outputs?

Output

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.

Trace 5 — Nested REPEAT
total 0 REPEAT 3 TIMES REPEAT 2 TIMES total total + 1 DISPLAY(total)

How many times does the innermost body execute?

Output

6 Outer loop: 3 times. Inner loop: 2 times per outer iteration. Total: 3 x 2 = 6 executions. total = 6.

Spot the Bug

SPOT THE BUG — Off By One: Wrong Initial Value
# Intended: add 1 through 5 (total = 15) total 1 REPEAT 5 TIMES total total + 1 DISPLAY(total)
Bug Explained

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.

SPOT THE BUG — Loop Count vs. Final Value Confusion
# Intended: DISPLAY 10 after doubling 3 times from 1 n 1 REPEAT 3 TIMES n n + 3 DISPLAY(n)
Bug Explained

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

1
REPEAT N TIMES runs exactly N times — not N-1, not N+1

If N is 5, the body executes 5 times. Students frequently count incorrectly when tracing. Number each iteration explicitly.

2
Forgetting to initialize accumulator variables before the loop

Any variable you update inside the loop must be assigned a starting value before the loop. Uninitialized variables are errors.

3
Thinking REPEAT 0 TIMES causes an error

REPEAT 0 TIMES is valid — the body simply never executes. The variable retains its pre-loop value.

4
Confusing loop count with the final variable 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
n starts at 0. Four iterations each add 5: 0+5+5+5+5 = 20.

2. How many times does the body of REPEAT 7 TIMES execute?

  • 6
  • 7
  • 8
  • Depends on the body
REPEAT N TIMES executes the body exactly N times. 7 times, always.

3. x ← 2
REPEAT 3 TIMES
x ← x * x
DISPLAY(x)


What is displayed?

  • 8
  • 512
  • 256
  • 16
x=2 → 2*2=4 → 4*4=16 → 16*16=256. Wait — 3 iterations: x=4, x=16, x=256. Display: 256.

3. x ← 2
REPEAT 3 TIMES
x ← x * x
DISPLAY(x)


What is displayed after all 3 iterations?

  • 8
  • 512
  • 256
  • 16
x=2 → x=2*2=4 → x=4*4=16 → x=16*16=256. DISPLAY(x) after loop = 256.

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")
REPEAT 5 TIMES executes the body 5 times. The last option prints ‘hello’ only once (after the loop ends, only one DISPLAY runs).

5. total ← 10
REPEAT 3 TIMES
total ← total - 4
DISPLAY(total)


What is displayed?

  • -2
  • 2
  • -6
  • 10
total=10 → 10-4=6 → 6-4=2 → 2-4=-2. Display: -2.

6. Consider:

REPEAT 3 TIMES
REPEAT 3 TIMES
DISPLAY("*")


How many stars are displayed?

  • 3
  • 6
  • 9
  • 12
Outer loop: 3 iterations. Inner loop: 3 iterations each. Total DISPLAY calls: 3 x 3 = 9 stars.

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
I is correct. II is correct — variables persist between iterations. III is false — REPEAT 0 TIMES is valid and simply executes the body zero times.

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.
If the accumulator starts at 1 instead of 0, the sum is N higher than expected because the initial 1 is extra. This is the classic accumulator initialization error.

9. count ← 0
REPEAT 10 TIMES
count ← count + 2
DISPLAY(count)


What is displayed?

  • 10
  • 12
  • 20
  • 22
count starts at 0 and adds 2 ten times: 0+2*10 = 20.

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
total = 0 + 5*N = 35 → N = 35/5 = 7.

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

Can N be a variable in REPEAT N TIMES?
Yes. REPEAT count TIMES where count is a variable uses count’s current value as the number of iterations. The count is locked in when the loop starts — changing count inside the loop does not affect how many times it runs.
How is REPEAT N TIMES different from FOR EACH?
REPEAT N TIMES runs a fixed number of times. FOR EACH iterates over each element in a list — the number of iterations equals the list length. Use REPEAT N TIMES when you know the count; use FOR EACH when processing a list.

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.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

tanner@apcsexamprep.com

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at tanner@apcsexamprep.com