Iteration (repetition) allows a block of code to run multiple times without having to rewrite it. AQA specifies three loop types: FOR, WHILE, and REPEAT…UNTIL.
Use when you know exactly how many times the loop should run.
The loop variable i automatically increments from the start value to the end value (inclusive). The default step is 1.
Use when the number of repetitions is not known in advance. The condition is checked before each iteration — so the body may never run at all if the condition starts False.
Input validation is a classic WHILE use case — keep asking until valid input is received:
The condition is checked after each iteration — so the body always runs at least once.
Keeps asking until the password is correct. Perfect when you want the user to enter data at least once before checking validity.
| Loop | When to use | Condition checked | Min runs |
|---|---|---|---|
| FOR | Know exact count | Start/end of count | 0 (if start > end) |
| WHILE | Don't know count; check first | Before each iteration | 0 |
| REPEAT…UNTIL | Don't know count; run first | After each iteration | 1 |
A loop with a condition that is always True (or never becomes False) will run forever — this is a bug. Common cause: forgetting to update the loop variable inside a WHILE loop.
8 questions · 24 marks
| Term | Definition |
|---|
Timed exam conditions.