Sub topic 8
Tracing Loops Iteration by Iteration
This chapter assumes you can read pseudocode with ← assignment, FOR and WHILE loops, and that you know how to set up a trace table with one column per variable. Here you use that table to follow a loop one pass at a time and to check that the loop actually stops.
The rhythm of a loop trace
Every loop repeats the same cycle: test the condition, run the body, then test again. A reliable trace records that cycle explicitly. Write one row for each test of the condition, including the final test that fails. Most tracing mistakes come from skipping that last test or from updating variables in the wrong order inside the body.
A worked WHILE loop
This procedure adds up the digits of a non-negative integer. MOD gives the remainder and DIV gives whole-number division.
FUNCTION DigitSum(n)
total ← 0
WHILE n > 0 DO
total ← total + n MOD 10
n ← n DIV 10
END WHILE
RETURN total
END FUNCTIONHere is the trace for n = 472. Each row shows the values after the body has run.
test | n before | n > 0 | n MOD 10 | total | n after
-----+----------+-------+----------+-------+--------
init | - | - | - | 0 | 472
1 | 472 | true | 2 | 2 | 47
2 | 47 | true | 7 | 9 | 4
3 | 4 | true | 4 | 13 | 0
4 | 0 | false | - | 13 | 0
Returns 13Row 4 matters. It shows why the loop stopped and confirms that total was not changed after the last digit.
Tracing FOR loops
A FOR loop hides its update and its test, so make them visible. FOR i ← a TO b runs b − a + 1 times when b ≥ a, and zero times when b < a. Before you trace, write down how many iterations you expect, then check that your table has that many body rows.
Checking termination
Tracing one input shows that the loop stopped for that input. To argue that it stops for every input, find a quantity that:
| Property | DigitSum example |
|---|---|
| Changes on every iteration | `n` is divided by 10 |
| Moves strictly toward a bound | `n` gets strictly smaller while `n > 0` |
| Cannot pass the bound forever | `n` is a whole number and cannot go below 0 |
If you cannot name such a quantity, look carefully for an infinite loop. Common causes are a body that never changes a variable used in the condition, a step that jumps over the stopping value, and an update that moves in the wrong direction.
Common errors
- Updating
nbefore usingn MOD 10, which skips the last digit. The order of lines in the body matters. - Recording only the passes where the condition is true and leaving out the final failing test.
- Assuming a WHILE loop runs at least once. With
n = 0,DigitSumnever enters the body and returns 0.
Exercise
Trace this loop and say whether it terminates: i ← 1; WHILE i ≠ 10 DO i ← i + 2 END WHILE. If it does not, give a one-character change to the condition that fixes it.
Show answer
The values of i are 1, 3, 5, 7, 9, 11, 13, and so on. i is always odd, so it never equals 10 and the loop never ends. Change ≠ to < so the condition reads i < 10. The loop then exits when i = 11.
Exercise
How many times does the body of FOR k ← 3 TO 7 run, and how many times does the body of FOR k ← 7 TO 3 run?
Show answer
The first runs 7 − 3 + 1 = 5 times. The second runs 0 times, because the start value is already past the end value.