Skip to content
Marlbridge

AP® Computer Science Principles · Unit 3 · Topic 3.8

Iteration: Revision Notes (Computer Science Principles 3.8)

One-page recap of iteration: REPEAT n TIMES versus REPEAT UNTIL, zero passes, infinite loops, counters and accumulators, and a method for tracing loops.

Recap

  • Iteration repeats a block zero or more times until a stopping condition is met.
  • REPEAT n TIMES { ... } runs the block exactly n times.
  • REPEAT UNTIL(condition) { ... } checks the condition before each pass, runs the block while it is false, and stops when it becomes true.
  • If the condition is true at the start, the block runs zero times.
  • A condition that can never become true makes an infinite loop.
  • A counter adds a fixed amount each pass; an accumulator builds a running total.

Key relationships

Pattern Code Result
Fixed repeat REPEAT 5 TIMES { x ← x + 2 } x increases by 10
Counter i ← i + 1 i counts the passes
Accumulator total ← total + n total builds a sum
Loop until goal REPEAT UNTIL(balance ≥ 150) Stops once the goal is reached
Zero passes Condition already true Block never runs
Infinite loop REPEAT UNTIL(n = 0) with n stepping by 3 from 10 Never ends

Assumptions

  • The condition is evaluated before every pass.
  • Variables change only through assignments in the block.
  • DISPLAY outputs are listed in the order they happen.

Method for tracing a loop

  1. Draw columns for every variable and for the condition.
  2. Write the starting values in the first row.
  3. Check the condition (REPEAT UNTIL) or count the pass (REPEAT n TIMES).
  4. Run the block statements one by one, updating the row.
  5. Repeat until the stop; note how many times the block ran.

Mistakes to avoid

  • Reading REPEAT UNTIL as “while true”.
  • Forgetting that the block can run zero times.
  • Using = as a stopping test where the variable can jump past the value.
  • Putting the counter update inside an IF that may not be true.
  • Off-by-one slips: n ≥ 150 versus n > 150.

Worked mini-example

n ← 1
count ← 0
REPEAT UNTIL(n ≥ 100)
{
  n ← n * 2
  count ← count + 1
}
DISPLAY(count)
DISPLAY(n)

Output: 7 128

n goes 2, 4, 8, 16, 32, 64, 128. After the 7th pass n is 128, which is at least 100, so the loop stops. count is 7.

Quick self-check

  1. How many times does REPEAT UNTIL(x > 5) run its block when x starts at 8?
  2. What does x hold after x ← 1 and REPEAT 3 TIMES { x ← x * 3 }?
  3. Why is REPEAT UNTIL(n = 0) risky when n decreases by 3 from 10?
Answers
  1. Zero times: the condition is already true.
  2. 27 (1 × 3 × 3 × 3).
  3. n steps over 0 (10, 7, 4, 1, −2, …), so the condition never becomes true and the loop never ends.

Where marks are won and lost

Typical items give a loop with a counter and ask for the final value or the number of passes. Trace it row by row and watch the exact stopping condition (≥ versus >). Another item asks which segment never ends, or which condition replaces a broken one. Robot questions ask for the final square or which loop reaches a goal. In the Create task your program must contain iteration, and you must explain what the loop does and why it ends.

Back to the full study guide for worked examples and explanations.

AP® and Advanced Placement® are trademarks registered by the College Board, which is not affiliated with, and does not endorse, this website. Marlbridge is an independent tuition provider. It is not an authorized AP course provider, an AP school or an exam centre, and these resources are not official College Board materials. Reading them or doing simulations does not meet the laboratory requirement of an AP science course.