Knowledge Organiser: Iteration
Key Terms
- Iteration: Repeating a block of code — also called a loop
- FOR loop: A count-controlled loop that repeats a fixed number of times
- WHILE loop: A condition-controlled loop that repeats while a condition is true (runs zero or more times)
- UNTIL loop: A loop that always runs at least once, then checks the condition (REPEAT UNTIL on AQA, DO UNTIL on OCR)
- Infinite loop: A loop whose condition never becomes false — a common bug
- Off-by-one error: A bug caused by using the wrong loop boundary (e.g. 1 to 10 vs 0 to 9)
Must-Know Facts
- FOR loops are used when the number of iterations is known in advance
- WHILE loops are used when the number of iterations is unknown
- A WHILE loop may execute zero times if the condition is false at the start
- An UNTIL loop always executes the body at least once before checking the condition
- WHILE loop conditions must eventually become false to avoid an infinite loop
Key Concepts
- FOR loop: for i = 1 to 5 ... next i
- WHILE loop: while condition ... endwhile
- UNTIL loop: do ... until condition (OCR) or REPEAT ... UNTIL condition (AQA)
- Input validation pattern: do ... INPUT age ... until age >= 0 AND age <= 120
- Sum 1 to 100: total = 0 ... for i = 1 to 100 ... total = total + i ... next i
Common Mistakes
- Using a FOR loop when the number of repetitions is unknown: FOR loops are for a fixed number of iterations — use WHILE or an UNTIL loop when repeating until a condition is met (e.g. user input validation)
- Confusing WHILE and UNTIL loops: A WHILE loop may run zero times if the condition is false from the start; an UNTIL loop always runs at least once — this matters when validating user input
- Creating infinite loops by never updating the condition variable: If the variable controlling a WHILE loop never changes inside the loop, the condition never becomes false
- Off-by-one errors in FOR loops: for i = 1 to 5 runs 5 times (1,2,3,4,5); for i = 0 to 4 also runs 5 times — confusing the two is a very common bug
- Not indenting loop bodies in pseudocode: Indentation is required to show which statements are inside the loop — examiners expect it in written pseudocode answers