The Stacks
BIG IDEA 3: ALGORITHMS AND PROGRAMMING · TOPIC 3.8

3.8 Iteration

Iteration repeats code. The exam's favorite questions: how many times does the loop run, and what's the final value when it stops?

What you need to know

  • Iteration (looping) is the third building block of algorithms. The code inside the loop is the loop body.
  • REPEAT n TIMES { body } runs the body exactly n times.
  • REPEAT UNTIL (condition) { body } checks the condition before each pass. If the condition is already true at the start, the body runs zero times. Otherwise it runs, then re-checks, until the condition becomes true.
  • FOR EACH item IN list { body } runs the body once per element, in order, with item holding the current element (covered more in 3.10).
  • An infinite loop occurs when a REPEAT UNTIL condition can never become true. The exam asks you to spot the variable that never changes.
  • The typical pattern: initialize a counter or accumulator before the loop, update it inside, and use it after. Tracing = table with one row per iteration.
  • Loop variables retain their final value after the loop ends. If a REPEAT UNTIL stops when i = 5, i is 5 afterward.

Worked example

i ← 1
sum ← 0
REPEAT UNTIL (i > 4)
{
    sum ← sum + i
    i ← i + 1
}
DISPLAY(sum)
DISPLAY(i)
check i > 4?sumi after
1 > 4 false12
2 > 4 false33
3 > 4 false64
4 > 4 false105
5 > 4 true → stop

Output: 10 5. Four iterations; i ends at 5, not 4.

Trace it yourself

Step through with the buttons, or use the ← → keys. Changed variables are highlighted.

Exam tip: REPEAT UNTIL runs while the condition is false. Students constantly flip this. Read it as "keep going until this becomes true." And always check the initial condition — a loop that's already satisfied runs zero times.

Going deeper

The nuance, edge cases, and connections that turn a 3 into a 5.

  • REPEAT UNTIL checks its condition before each iteration, including the first. If the condition is already true, the body runs zero times. (Some real languages have do-while loops that run the body at least once; AP pseudocode's REPEAT UNTIL does not.)
  • The condition is a stopping condition: the loop continues while it's false and stops when it becomes true. Students used to "while" loops must mentally negate. REPEAT UNTIL (i > 4) is the same as "while i ≤ 4."
  • REPEAT n TIMES runs exactly n times; there's no loop variable you can read. If you need a counter, declare and increment one yourself.
  • Infinite loop detection: find the variables in the stopping condition, then check whether the body ever changes them in the direction that would make the condition true. If the condition is i > 10 and the body does i ← i - 1, it never stops (assuming i starts ≤ 10).
  • The final value of a loop variable after the loop is the value that made the condition true — one step past the last iteration's value. If the body ran with i = 1, 2, 3, 4, then i is 5 afterward.
  • The exam's block-based pseudocode draws REPEAT UNTIL as a shape with the condition on top. Same semantics.
  • Combining a loop with an accumulator (sum ← sum + i) is how sums, counts, and products are computed. The accumulator must be initialized before the loop.

Mistakes that cost points

  • Running the body while the condition is true. REPEAT UNTIL is the opposite — body runs while false. This one flip is responsible for a large share of wrong loop answers.
  • Forgetting the zero-iteration case. If the stopping condition is true before the loop starts, nothing inside runs. Check the initial state first.
  • Reporting the last iteration's value instead of the final value. The variable's final value is the one that stopped the loop, which is one step beyond.
  • Not initializing the accumulator. A sum that's never set to 0 before the loop is undefined. On the exam it's a logic error to spot.

Practice questions

Written in the style of the real exam. Try each one before revealing the answer.

Q1 How many times is "hi" displayed by the following code?
count ← 10
REPEAT UNTIL (count < 4)
{
    DISPLAY("hi")
    count ← count - 2
}
  1. A 3
  2. B 4
  3. C 5
  4. D The loop never ends
Show answer

Answer: B. count goes 10 → 8 → 6 → 4 → 2. The body runs while count ≥ 4: for values 10, 8, 6, 4 — four times. When count is 2, the condition is true and the loop stops.

Q2 Which of the following loops will never terminate?
  1. A x ← 0
    REPEAT UNTIL (x ≥ 5) { x ← x + 1 }
  2. B x ← 10
    REPEAT UNTIL (x = 0) { x ← x - 2 }
  3. C x ← 3
    REPEAT UNTIL (x > 100) { x ← x * 2 }
  4. D x ← 1
    REPEAT UNTIL (x = 0) { x ← x + 1 }
Show answer

Answer: D. In D, x starts at 1 and only increases, so x = 0 can never become true. Option B does terminate: 10, 8, 6, 4, 2, 0.

Q3 What is the value of total after the following code runs?
total ← 0
REPEAT 3 TIMES
{
    total ← total + 5
    total ← total * 2
}
  1. A 30
  2. B 50
  3. C 70
  4. D 150
Show answer

Answer: C. Pass 1: 0+5=5, ×2=10. Pass 2: 15, ×2=30. Pass 3: 35, ×2=70.

Key vocabulary

Iteration
repeating a block of code
Loop body
the statements inside a loop that are repeated
REPEAT UNTIL
a loop that runs while its condition is false and stops once it becomes true
Infinite loop
a loop whose exit condition is never met