Phase 1: Prepare
Step 1 Warm-up recall
From M3.L3: where should a running total start?
Private: nothing you type here is sent or stored.
Step 2 Learning goal
By the end of this lesson you can:
- choose for or while for a task (Foundation);
- trace a loop with a table (Core);
- spot an off-by-one error (Core).
Phase 2: Teach
Step 3 A familiar example
A schedule with a fixed number of days uses for. A job that repeats until a stock level runs out uses while. The first knows how many in advance; the second does not.
Step 4 Clear explanation
Use a for loop when you know how many times to repeat, or you have a sequence to walk through. Use a while loop when you repeat until something changes and you do not know how many passes it takes.
To check any loop, trace it by hand with a table, one row per pass:
x = 10
while x > 0:
x = x - 4
print(x)| Pass | x at start | x after x = x - 4 | Printed | x > 0 ? |
|---|---|---|---|---|
| 1 | 10 | 6 | 6 | True |
| 2 | 6 | 2 | 2 | True |
| 3 | 2 | -2 | -2 | False, loop ends |
6
2
-2Note that -2 is printed, because the check happens before the pass, not after the update. The most common loop bug is the off-by-one error: a loop that runs one time too many or too few.
A loop can sit inside another. For each pass of the outer loop, the inner loop runs completely:
for i in range(1, 3):
for j in range(1, 4):
print(i * j)1
2
3
2
4
6Here the outer loop runs 2 times and the inner 3 times each, so the print line runs 2 × 3 = 6 times.
Step 5 Worked example (4 marks)
How many lines does for a in range(3): for b in range(4): print(a, b) print?
- The outer loop runs 3 times. (1 mark)
- The inner loop runs 4 times for each of them. (1 mark)
- 3 × 4 = 12. (1 mark)
- Answer: 12 lines. (1 mark)
Go deeper: the professor's notes
Is it right?
Is it right?
Does error grow?
Error
| Hours | Error (s) |
|---|---|
| 8 | 0.0275 |
| 100 | 0.3433 |
At work: A long-running process
It runs for days without a restart.
- Trace the first hour.
- Trace the last hour.
What a careful practitioner decides. Test long runs, not only short ones.
Phase 3: Practise
Step 6 Learner practice (instant-marked)
1. You know the number of passes in advance. Use:
for fits a known count.
2. You repeat until the user enters a valid value. Use:
The count is unknown.
3. An off-by-one error is a loop that runs:
It is wrong by exactly one pass.
4. An outer loop of 2 and an inner loop of 5 run the inner body:
2 × 5.
Phase 4: Close
Step 7 Recap
- for for a known count; while for an unknown count.
- Trace with a table, one row per pass.
- Check the first and last pass for off-by-one errors.
Step 8 Exit ticket (ungraded)
Trace x = 7, while x > 0: x = x - 3, print(x). List the printed values.
Private: nothing you type here is sent or stored.
Step 9 Go further (Extension)
Trace the nested loop with range(1, 4) outside and range(1, 3) inside.
In industry: Patriot: the error grows with the count
The Patriot clock error was about 0.0275 seconds after 8 hours and about 0.3433 seconds after 100 hours. The same tiny rounding was repeated at every tick.
Link to this lesson. Trace a loop for the first pass and for the last pass. The two can look very different.
Source: Skeel, Roundoff Error and the Patriot Missile; GAO/IMTEC-92-26
Common mistakes
| Mistake | Why it fails |
|---|---|
| Skipping the trace | Run it by hand before running it on the machine. |
| Counting values as stop minus one | range(1, 5) has 4 values, not 5. |
| Mixing up inner and outer | The inner loop finishes first on every outer pass. |
Limits. Tracing shows what a program does for one input; use several inputs.