Spec & Goals 3 min
AQA Spec 3.2.2 · Programming constructs — iteration
By the end of this lesson you can:
- Explain what iteration is and write a count-controlled
FORloop. - Write condition-controlled loops using
WHILE … ENDWHILEandREPEAT … UNTIL. - Choose the right loop for a problem and trace a loop to find its output.
Warm-Up 5 min
Last lesson selection chose which code runs. Iteration decides how many times code runs.
Quick starter
A teacher marks 30 exam papers, one after another. Which describes this best?
- Repeating a task a known number of times.
- Repeating until something becomes true.
- Doing the task only once.
Reveal the answer
Repeating a task a known number of times (30). That is a count-controlled loop — a job for FOR.
Key Concept — repeating code with loops 14 min
Iteration means repeating a block of code. The repeated block is called a loop. There are two kinds.
Count-controlled iteration — FOR
Use a count-controlled loop when you know how many times to repeat. A counter steps from a start to an end value.
AQA pseudo-code
FOR i ← 1 TO 5 OUTPUT i ENDFOR
Condition-controlled iteration — WHILE and REPEAT
Use a condition-controlled loop when you do not know the number of repeats in advance — it depends on a condition.
WHILE tests the condition before the loop body, so the body may run zero times:
WHILE password ≠ 'open' DO password ← USERINPUT ENDWHILE
REPEAT tests the condition after the loop body, so the body always runs at least once:
REPEAT password ← USERINPUT UNTIL password = 'open'
Worked Example — a sum and a validation loop 12 min
Problem 1: use a FOR loop to add the numbers 1 to 5 and output the total.
AQA pseudo-code
total ← 0 FOR i ← 1 TO 5 total ← total + i ENDFOR OUTPUT total
The same loop in Python
total = 0 for i in range(1, 6): total = total + i print(total)
Trace the loop. After the loop, total is 15:
| i | total + i | total |
|---|---|---|
| 1 | 0 + 1 | 1 |
| 2 | 1 + 2 | 3 |
| 3 | 3 + 3 | 6 |
| 4 | 6 + 4 | 10 |
| 5 | 10 + 5 | 15 |
Problem 2: keep re-asking for an age until the user gives a valid one (1–120). The body must run at least once, so use REPEAT … UNTIL.
REPEAT age ← USERINPUT UNTIL age ≥ 1 AND age ≤ 120 OUTPUT 'Thank you'
while True: age = int(input("Enter your age: ")) if age >= 1 and age <= 120: break print("Thank you")
Try It Yourself 12 min
Goal: Write a FOR loop that outputs the numbers 1 to 10.
Hint: use FOR i ← 1 TO 10 … ENDFOR.
Goal: Write a WHILE loop that keeps adding RM 50 to balance (starting at 0) and outputs it, while balance < 200.
Hint: test the condition before each repeat.
Goal: Use a FOR loop to multiply 1 × 2 × 3 × 4 × 5 (a factorial) and output the result.
Hint: start a product variable at 1, not 0.
📝 Exam Practice 10 min
Answer the way the examiner expects — the command word and the marks tell you how much to write.
State the difference between count-controlled and condition-controlled iteration.
Mark scheme
- Count-controlled repeats a known / fixed number of times (1).
- Condition-controlled repeats while/until a condition is met / an unknown number of times (1).
Give one situation where REPEAT … UNTIL is more suitable than WHILE.
Mark scheme
- When the loop body must run at least once, e.g. ask for input then validate it (1). Accept any valid example.
Complete the loop so it outputs every number from 2 to 8.
FOR count ← __________ TO __________ OUTPUT __________ ENDFOR
Mark scheme
- Start value
2(1). - End value
8(1). OUTPUT count(1).
Look again at the FOR sum example. State the value of total if the loop runs FOR i ← 1 TO 3 instead.
Mark scheme
6(1) — 1 + 2 + 3.
Recap & Key Terms 3 min
Iteration repeats code. Use a count-controlled FOR loop for a known number of repeats. Use a condition-controlled loop — WHILE (tests before) or REPEAT … UNTIL (tests after) — when the number of repeats is unknown.
- Iteration
- Repeating a block of code one or more times.
- Count-controlled
- A loop that repeats a known, fixed number of times.
- Condition-controlled
- A loop that repeats while/until a condition is met, an unknown number of times.
- FOR
- A count-controlled loop:
FOR i ← 1 TO n … ENDFOR. - WHILE
- A condition-controlled loop that tests its condition before the body, so it may run zero times.
- REPEAT-UNTIL
- A condition-controlled loop that tests its condition after the body, so it runs at least once.
Homework 1 min
Task (≤ 15 min): Write AQA pseudo-code that uses a WHILE loop to output the 5 times table (5, 10, …, 50).
Model answer
count ← 1 WHILE count ≤ 10 DO OUTPUT count * 5 count ← count + 1 ENDWHILE
Award marks for: correct loop and condition (1), output of count * 5 (1), increment the counter so the loop ends (1).