Standard Algorithm Structures · Cambridge syllabus 7.9, 8.1 · Lesson 3 of 3
Iteration
Iteration repeats instructions. A FOR loop suits a known count, WHILE checks before each repetition and may run zero times, and REPEAT UNTIL checks after the body and runs at least once. Update the controlling state so the loop can finish.
By the end, you can
- Understand fixed-count loops.
- Use FOR loops, including a STEP increment.
- Use WHILE loops.
- Use REPEAT UNTIL loops.
- Choose an appropriate loop for a problem.
Loops
Iteration repeats a block of instructions. Which loop to use depends on one question: is the number of repetitions known before the loop starts?
A FOR loop is a count-controlled loop and is the right choice when the number is known. FOR i ← 1 TO 10, the block, NEXT i runs the block exactly ten times, with the counter set up, increased and tested automatically. Processing every element of an array of known size is the standard case.
A FOR loop can also count in steps other than 1, using STEP. FOR Count ← 0 TO 20 STEP 5 gives Count the values 0, 5, 10, 15 and 20, and FOR Count ← 10 TO 1 STEP −1 counts down from 10 to 1. The loop ends when the next value would go past the end value, so FOR Count ← 1 TO 10 STEP 4 runs for 1, 5 and 9 only. Without STEP, the counter goes up by 1.
A WHILE loop is condition-controlled and tests before the block runs, so the block may never run at all. WHILE Stock > 0 DO, the block, ENDWHILE is right when the repetition depends on something that might already be satisfied — and if no items are in stock, nothing should happen.
A REPEAT UNTIL loop tests after the block, so it always runs at least once. REPEAT, INPUT Password, UNTIL Password = "Secret" is right for input validation, because asking once is unavoidable.
Two errors are almost universal. An infinite loop, where nothing inside the block can ever make the condition false — usually because the variable in the condition is never changed. And an off-by-one error, where the loop runs one time too many or too few because the range was written 1 TO n when it should have been 0 TO n − 1, or the test used < where <= belongs.
Worth remembering
- FOR when the number of repetitions is known; WHILE or REPEAT when it is not.
- WHILE tests first and may run zero times; REPEAT tests last and always runs once.
- Make sure something inside the loop can change the condition, or it never ends.
- STEP sets how much a FOR loop’s counter changes each time, and it can be negative.
Worked example
A WHILE loop asking for valid input must read another value when the old value is invalid.
Practice
Warm-up · not marked
Check your understanding
Answer in your own words, then compare with the worked answer. Nothing here is saved or scored.
Worked answer
REPEAT UNTIL is a natural choice because the body runs before the condition is tested.
Check your method, units and explanation against it.
Marked practice
Set 1 of 5 · easiest first · Checked as you go, with the worked answer. Sign in or create a free account to save your progress.