Article
Tracing a loop
The exam gives you a loop and asks, "What is displayed?" You cannot guess it from the shape of the code. You have to be the computer: run the code by hand, one step at a time, and write down every value. This is called tracing, and it is the most useful programming skill on the exam.
Five steps for tracing a loop
- Write the name of every variable as a column in a table.
- Fill in the starting values from the lines before the loop.
- Before each pass, check the condition (for
REPEAT UNTIL, stop if it is true). ForREPEAT n TIMES, count the passes. - Run the body line by line, writing the new value of any variable that changes.
- When the loop ends, carry on with the lines after it. Write down everything
DISPLAYshows, in order.
A trace table has one row per pass. If two rows are exactly the same, the loop will never end.
Here is a loop with an IF inside it. It counts the even numbers (n MOD 2 = 0 means "n divided by 2 leaves remainder 0"):
count ← 0
n ← 1
REPEAT UNTIL(n > 6)
{
IF(n MOD 2 = 0)
{
count ← count + 1
}
n ← n + 1
}
DISPLAY(count)| Check | n | Stop? (n > 6) | n MOD 2 = 0? | count after the pass | n after the pass |
|---|---|---|---|---|---|
| 1 | 1 | false | false | 0 | 2 |
| 2 | 2 | false | true | 1 | 3 |
| 3 | 3 | false | false | 1 | 4 |
| 4 | 4 | false | true | 2 | 5 |
| 5 | 5 | false | false | 2 | 6 |
| 6 | 6 | false | true | 3 | 7 |
| 7 | 7 | true |
It displays 3. The Python version has the same table:
Check your understanding
What is displayed when this code runs?
n ← 1
REPEAT UNTIL(n > 5)
{
IF(n MOD 2 = 1)
{
DISPLAY(n)
}
n ← n + 1
}A loop inside a loop
The body of a loop can hold another loop. For every one pass of the outer loop, the inner loop runs all of its passes.
REPEAT 2 TIMES
{
REPEAT 3 TIMES
{
DISPLAY("*")
}
DISPLAY("-")
}The outer loop runs 2 times. Each time it shows three stars (the inner loop) and then a dash. So the output is * * * - * * * -. The star is displayed 2 × 3 = 6 times. The dash is displayed 2 times.
(end=" " tells Python to put a space after the value instead of starting a new line, so it looks like DISPLAY.)
Off-by-one errors
An off-by-one error is a loop that runs one pass too many or one too few. It is the most common loop bug, and the exam builds questions on it. Look at these two loops. They are the same except for the condition:
n ← 1
REPEAT UNTIL(n = 5)
{
DISPLAY(n)
n ← n + 1
}This shows 1 2 3 4. When n becomes 5, the loop stops before showing it. It runs 4 passes. With REPEAT UNTIL(n > 5) it would show 1 2 3 4 5, which is 5 passes.
The order of the lines matters too. Both of these stop when n > 3:
DISPLAY first | Add first |
|---|---|
DISPLAY(n) then n ← n + 1 | n ← n + 1 then DISPLAY(n) |
shows 1 2 3 (from n ← 1) | shows 2 3 4 (from n ← 1) |
To count passes: from a first value to a last value, both included, there are last − first + 1 passes. From 1 to 5 there are 5 passes, from 3 to 7 there are 5 passes, not 4. Python's range(1, 5) gives only 4 numbers (it leaves out the 5), so range(1, 6) is the one for 1 to 5.
Check your understanding
What is displayed when this code runs?
n ← 1
REPEAT UNTIL(n = 4)
{
DISPLAY(n)
n ← n + 1
}Worked example: a mixed trace
What is displayed?
a ← 20
steps ← 0
REPEAT UNTIL(a ≤ 3)
{
a ← a - 6
steps ← steps + 1
}
DISPLAY(steps)
DISPLAY(a)| Check | a | Stop? (a ≤ 3) | a after | steps after |
|---|---|---|---|---|
| 1 | 20 | false | 14 | 1 |
| 2 | 14 | false | 8 | 2 |
| 3 | 8 | false | 2 | 3 |
| 4 | 2 | true |
The loop ran 3 passes. It displays 3 2. Notice that a is 2, below the stopping value 3, because the check only happens before a pass.
Common mistakes
- Not writing the table. Doing it in your head is how values get mixed up. Write it down.
- Tracing the last check as a pass. The check that says "stop" does not run the body.
- Reading
DISPLAYas if it ran once. If it is inside the loop, it shows a value on every pass. - Forgetting the starting values. The table starts with the lines before the loop.
- Mixing up the inner and outer loops. The inner loop runs fully each time the outer loop runs once.
On the exam
Questions give the code and ask for the output, for the final value of a variable, or for how many times a line runs. A good answer comes from a table, not from a feeling. If you have a few seconds left, trace again with the smallest case, which is often 0 or 1 passes, to check the edges.
Key words
Tracing: running code by hand and recording each variable's value.
Trace table: the table of those values, one row per pass.
Nested loop: a loop inside the body of another loop.
Off-by-one error: a loop that runs one pass too many or too few.