Topic 8 Β· Programming Β· 8.7

Iteration Explained Simply

Iteration means repeating instructions. A loop keeps running code until a number of repeats is reached or a condition tells it to stop.

Invitation

How can a program repeat a task?

Programs often need to perform the same instruction more than once. Writing the same line again and again would be slow and difficult to maintain.

Iteration is the programming construct used to repeat a set of instructions.

Repeat the instructions. Check when to stop.
Figure 1
A repeating process
Run instructions
↓
Check progress
β†Ί
Repeat or stop
Big Idea

A loop controls repetition

A loop contains instructions that can run several times. The loop must also have a clear way to finish.

Some loops repeat a known number of times. Others repeat while, or until, a condition is met.

Every loop needs instructions to repeat and a stopping point.
Figure 2
Inside a loop
Start
↓
Repeat instructions
↓
Stop condition reached?
FutureLogic Bridge

Iteration is like running laps

Imagine running around a track. Each lap repeats the same route.

You might stop after five laps, continue while you still have time, or complete one lap before checking whether the session is over.

One lap is one iteration. The stopping rule decides when the runner finishes.
Figure 3
The running-track bridge
Start lap
β†’
Run around track
β†Ί
Another lap?
Worked Example

Output the numbers 1 to 5

The program knows in advance that the instructions must repeat five times, so a count-controlled loop is suitable.

PseudocodeCount-controlled loop
FOR Count <- 1 TO 5
    OUTPUT(Count)
NEXT Count
PythonEquivalent idea
for count in range(1, 6):
    print(count)

Follow the loop

The variable Count takes the values 1, 2, 3, 4 and 5.

Output: 1 2 3 4 5
Three Loop Types

Choose the loop that matches the task

Count-controlledThe number of repetitions is known before the loop begins.
Pre-conditionThe condition is checked before the instructions run. The loop may run zero times.
Post-conditionThe condition is checked after the instructions run. The loop runs at least once.
FOR = known number. WHILE = check first. REPEAT...UNTIL = check last.
Exam Tip

Name the loop and describe how it works

Exam questions often ask for a type of iteration and a description. Naming the loop alone may not earn every mark.

Strong answer: β€œA pre-condition loop checks its condition before the loop body, so it may not execute at all.”

Use the words before, after, or pre-determined to make the difference clear.

Common Mistake

β€œEvery loop runs at least once.”

A pre-condition loop checks first. If its condition is already false, its instructions do not run.

Incorrect: β€œA WHILE loop always runs once.”
Correct: β€œA WHILE loop may run zero times because its condition is checked before the loop body.”
Summary

The repetition construct

IterationRepeats a group of instructions.
Count-controlledRepeats a known number of times.
Pre-conditionChecks before each repetition.
Post-conditionChecks after each repetition.
Loop bodyThe instructions being repeated.
Stopping rulePrevents the loop continuing forever.