Skip to content
Marlbridge

AP® Computer Science Principles · Unit 3 · Topic 3.8

Iteration: Study Guide (Computer Science Principles 3.8)

How REPEAT n TIMES and REPEAT UNTIL work, how to trace loops with a table, how counters and accumulators behave, and why loops end or fail to. Two worked examples including infinite loops.

Computers are good at one thing humans find tiring: doing the same job again and again without getting bored. Iteration is the repetition of a portion of an algorithm. Instead of writing the same statements ten times, you write them once inside a loop and tell the computer how long to keep going: either a fixed number of times or until something becomes true. This topic covers the two loop forms in the exam notation, REPEAT n TIMES and REPEAT UNTIL, how to trace loops, and the classic bug of the loop that never ends.

Notation. The exam uses its own pseudocode, not a real programming language. The code in this pack is Marlbridge’s rendering of that reference notation, not a copy of the official sheet: ← is assignment, = tests equality, lists start at index 1, DISPLAY shows a value, and braces mark blocks. Use the Exam Reference Sheet from your teacher for the official wording.

Three building blocks, third part

Every algorithm can be built from sequencing (steps in order), selection (choosing, Topics 3.6 and 3.7) and iteration (repeating). Iteration statements change the sequential flow of control by repeating a set of statements zero or more times, until a stopping condition is met. An algorithm that uses iteration can also be described without any programming language, for example “keep stirring until the sauce thickens”.

REPEAT n TIMES

REPEAT n TIMES
{
  <block of statements>
}

The block runs exactly n times. The count n is worked out once, when the loop starts. This form is ideal when you know in advance how many repetitions you need, such as “add the number ten times”. If n is 0 the block does not run at all.

REPEAT UNTIL(condition)

REPEAT UNTIL(condition)
{
  <block of statements>
}

The block repeats until the Boolean condition evaluates to true. Three facts matter for the exam:

  1. The condition is checked before each pass. If it is already true when the loop is reached, the block runs zero times.
  2. The loop keeps going while the condition is false. (Do not confuse it with a “while” loop in some languages, where the loop runs while the condition is true.)
  3. If the condition can never become true, the result is an infinite loop: the program never finishes.
Flow of control in REPEAT UNTIL A flowchart. A diamond holds the condition and is checked first. If the condition is false, the flow goes right to a box that runs the block and changes variables, then loops back to check the condition again. If the condition is true, the flow leaves the loop through a dashed box on the right. The check always happens before the block runs. conditiontrue?falserun the block;variables changethen check againtrueleave the loop
Figure 1. The test comes first. While it is false the block runs and the test is repeated; the moment it is true the loop is left.

Real loops almost always change a variable inside the block so that the condition eventually becomes true. A counter adds one on each pass; an accumulator builds up a total. Both patterns come from Topic 3.1: the variable on the left of ← also appears on the right.

Worked example 1: a counted loop

Find what this segment displays.

total ← 0
n ← 1
REPEAT 4 TIMES
{
  total ← total + n
  n ← n + 2
}
DISPLAY(total)

Output: 16

Step 1. Trace table. One row per pass; the values are those after the pass.

Pass total before n before total after n after
1 0 1 1 3
2 1 3 4 5
3 4 5 9 7
4 9 7 16 9

Step 2. After four passes total is 16, so 16 is displayed.

Check. The block adds the first four odd numbers: 1 + 3 + 5 + 7 = 16 ✓. (The sum of the first k odd numbers is always k², a nice sanity check: 4² = 16.)

A loop with a decision inside. Combine iteration with selection to count items. This segment counts how many of the numbers 1 to 10 are multiples of 3:

count ← 0
i ← 1
REPEAT 10 TIMES
{
  IF(i MOD 3 = 0)
  {
    count ← count + 1
  }
  i ← i + 1
}
DISPLAY(count)

Output: 3

The condition is true for i = 3, 6 and 9, so count ends at 3 ✓. Note that i ← i + 1 is outside the IF so that it happens on every pass.

Worked example 2: REPEAT UNTIL, zero passes and infinite loops

A savings account starts with 100 and gains 20 at the end of each year. How many years until the balance is at least 150?

balance ← 100
years ← 0
REPEAT UNTIL(balance ≥ 150)
{
  balance ← balance + 20
  years ← years + 1
}
DISPLAY(years)
DISPLAY(balance)

Output: 3 160

Check before pass balance balance ≥ 150 Action
1 100 false balance 120, years 1
2 120 false balance 140, years 2
3 140 false balance 160, years 3
4 160 true leave the loop

The condition is checked four times but the block runs only three. Output: 3 160.

Zero passes. If the balance already starts at 200, the first check is true and the block never runs: years stays 0.

Infinite loop. Consider this loop with n equal to 10:

REPEAT UNTIL(n = 0)
{
  n ← n - 3
}

n goes 10, 7, 4, 1, −2, −5 and so on. It steps over 0 without ever equalling it, so the condition is never true and the loop never ends. Starting with n equal to 9 would stop after three passes. A safer condition is n ≤ 0, which is true for 0 and for everything below it.

Check. Before running any REPEAT UNTIL, ask two questions: “Does the block change something the condition depends on?” and “Can the condition become true no matter what the start value is?”

Iteration with a robot

The exam reference sheet includes a grid robot with the procedures MOVE_FORWARD(), ROTATE_LEFT(), ROTATE_RIGHT() and the test CAN_MOVE(direction). Loops let a short program handle any size of grid. This segment walks a robot along an empty corridor until a wall is in front of it:

REPEAT UNTIL(NOT CAN_MOVE(forward))
{
  MOVE_FORWARD()
}

If the robot starts in the leftmost square of a corridor six squares long, facing right, the test is true (it can move) five times, so the block runs five times and the robot ends in the sixth square. Because the condition is checked before each move, the robot never tries to move into the wall, and the same code works for a corridor of any length.

Common misconceptions

  • “REPEAT UNTIL runs while the condition is true.” It runs while the condition is false and stops when it becomes true.
  • “The block always runs at least once.” Not for REPEAT UNTIL: if the condition is true at the start, it runs zero times.
  • “A loop condition is checked continuously.” It is checked once before each pass, not in the middle of the block.
  • “n = 0 is a safe stopping condition.” If n skips over 0 the loop never ends; use n ≤ 0 when stepping down.
  • “The loop variable updates itself.” Unless the block changes it, it stays the same, and an unchanged condition means an infinite loop or a loop that never starts.
  • “Placing the update inside an IF is the same as placing it outside.” Inside, it happens only when the condition is true, so the loop may never advance.

Where this leads

Iteration is combined with selection and lists in Topic 3.10, where FOR EACH and loops visit every element of a list, and it returns in Topic 3.17 when we compare how many steps different algorithms take.

Next steps: try the practice questions, skim the revision notes the night before, and tick off the topic checklist. The next topic is Topic 3.9, Developing Algorithms.

Quick questions

What is the difference between REPEAT n TIMES and REPEAT UNTIL?

REPEAT n TIMES runs a block a fixed number of times. REPEAT UNTIL keeps running the block until a condition becomes true, so the number of repetitions depends on the data.

Can a REPEAT UNTIL loop run zero times?

Yes. The condition is checked before each pass, so if it is already true at the start the block never runs.

What is an infinite loop?

A loop whose stopping condition never becomes true, so the program never finishes. For example, stepping a counter past a value it was meant to equal.

Does REPEAT UNTIL stop in the middle of the block?

No. The condition is checked only before each pass. Once the block starts it runs to the end.

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.