Mrs. Sri wrote the sales of the last three days in her notebook: $120, $95 and $140. The calculator says the total is $355. The program her nephew made prints a different number:
Total: 215
There is no error message. The program runs to the end, but the answer is wrong. A mistake in a program is called a bug. A bug like this one is the hardest kind to find, because the computer gives you no hint at all.
The surest way to find it: act as the computer yourself. Run the program on paper with a pencil, line by line, and write down what each variable holds in a table. That table is called a trace table.
One step, one row of the table
Start with a short program. Before you read on, guess: does it print 15 or 12?
price ← 5
count ← 3
total ← price * count
price ← 4
PRINT total
This is the trace table of that program. The Step column numbers each step, and the Line column gives the number of the program line that runs. Then there is one column for each variable, in the order they are first filled, and one Output column for what is printed. Each time a variable changes or something is printed, the table gets a new row. A cell (one box of the table) stays blank when its variable does not change in that step.
| Step | Line | price | count | total | Output |
|---|---|---|---|---|---|
| 1 | 1 | 5 | |||
| 2 | 2 | 3 | |||
| 3 | 3 | 15 | |||
| 4 | 4 | 4 | |||
| 5 | 5 | 15 |
This is what the program prints:
15
The answer is 15. In step 4, price becomes 4, but the total column does not change. total was calculated in step 3, and it stores the result, not the formula. To find what a variable holds at any step, read its column upwards from that step until you reach a filled cell.
The Playground can make the same table for you: press Run, then open the Trace table tab.
Tracing a loop
Now Mrs. Sri's program. Its input is four lines: the number of days, then each day's sales. With the input 3, 120, 95, 140:
READ n
total ← 0
day ← 1
WHILE day < n DO
READ sale
total ← total + sale
day ← day + 1
END WHILE
PRINT "Total:", total
Total: 215
This is its trace table. Inside a loop, the same program lines run again and again. That is why the Line column goes back to 5 in each round:
| Step | Line | n | total | day | sale | Output |
|---|---|---|---|---|---|---|
| 1 | 1 | 3 | ||||
| 2 | 2 | 0 | ||||
| 3 | 3 | 1 | ||||
| 4 | 5 | 120 | ||||
| 5 | 6 | 120 | ||||
| 6 | 7 | 2 | ||||
| 7 | 5 | 95 | ||||
| 8 | 6 | 215 | ||||
| 9 | 7 | 3 | ||||
| 10 | 9 | Total: 215 |
The sale column is filled only twice: 120 and 95. The third day's sales, 140, are never read. Why? Look at step 9: day becomes 3. Then the computer checks the condition on line 4, day < n. It is now 3 < 3, and that is FALSE. The loop stops, although the third day's sales have not been added.
Checking a condition does not add a row to the table, because no variable changes. But the bug is exactly in that check. So when you trace with a pencil, write the result of each check beside the table: 1 < 3 TRUE, 2 < 3 TRUE, 3 < 3 FALSE.
Off by one round
The third day's sales have to be read too. So the loop must keep running when day equals n. Change < to <=, and run it again. With the input 3, 120, 95, 140:
READ n
total ← 0
day ← 1
WHILE day <= n DO
READ sale
total ← total + sale
day ← day + 1
END WHILE
PRINT "Total:", total
Total: 355
A bug like this has a name: off-by-one. The loop runs one round too few, or one round too many. The cause is almost always a < mixed up with <=, or a start of 0 mixed up with 1. When the result is wrong by exactly one round, like the missing 140 in Mrs. Sri's program, trace the first round and the last round first.
One round, one row
A table with one row per step gets long quickly. For a loop, one table row per round is enough, with what the variables hold at the end of that round. This is the trace table of the fixed program:
| Step | Line or round | n | total | day | sale | Output |
|---|---|---|---|---|---|---|
| 1 | line 1 | 3 | ||||
| 2 | line 2 | 0 | ||||
| 3 | line 3 | 1 | ||||
| 4 | round 1 | 120 | 2 | 120 | ||
| 5 | round 2 | 215 | 3 | 95 | ||
| 6 | round 3 | 355 | 4 | 140 | ||
| 7 | line 9 | Total: 355 |
Look at round 3: day becomes 4, and 4 <= 3 is FALSE. The loop stops right after the third day's sales are added. In the Playground's Trace table tab, this shorter table appears when you switch on the option One turn, one row.
PRINT as a trace
Tracing a long program with a pencil is tiring. There is a shortcut: add a temporary PRINT where you suspect the problem is, so the computer itself writes down what the variables hold. This is Mrs. Sri's program before the fix, with a temporary PRINT on line 7. With the input 3, 120, 95, 140:
READ n
total ← 0
day ← 1
WHILE day < n DO
READ sale
total ← total + sale
PRINT "trace:", day, sale, total
day ← day + 1
END WHILE
PRINT "Total:", total
trace: 1 120 120
trace: 2 95 215
Total: 215
There are only two trace lines, but there are three days. The bug shows without a pencil. Once you have found and fixed the bug, delete the trace PRINT line again, so the output is clean.
⚠️ A column with too much, one that's stuck, one that's empty
A trace table also helps explain the errors that stop a program. These are the three errors you will see most often in loops:
- One round too many. If Mrs. Sri's program starts at
day ← 0and usesWHILE day <= n, the loop runs four times for three days. In the fourth round,READ salegets no input, and the program stops with the message "Line 5: READ needs more input, but the 4 lines of input are used up." In its trace table, thedaycolumn held 0, 1, 2 and 3: four values, four rounds. Fix it by starting from 1, or by using<. - A column that is stuck. A
WHILE day <= 7loop with onlytotal ← total + 1inside never changesday. In the trace table, thedaycolumn is filled once and then never changes, whiletotalkeeps going up. The computer stops it with the message "The loop on line 3 never stops:daydoesn't change inside it." Addday ← day + 1inside the loop. - A column that is still empty when it is used. Without the line
total ← 0, thetotalcolumn has never been filled whentotal ← total + saletries to add to it. The program stops with the message "Line 5:totalis used before it has a value." Filltotalabove the loop first:total ← 0.
TL;DR: A trace table is a table you fill in while you run a program on paper, line by line. It records what each variable holds every time it changes, plus what is printed, in the order the computer runs the lines. Use a trace table for a bug (a mistake in a program) that gives no error, only a wrong answer. In a loop, also write down the result of each condition check. Those notes usually show an off-by-one bug: a loop that runs one round too few or too many, for example because of a < that should be <=. For a long program, one table row per round is enough. Another way: add a temporary PRINT that prints the variables, then delete it afterwards. The Playground builds the trace table for you in its Trace table tab.
⚡ Be the computer yourself
Write:
price ← 5
count ← 3
total ← price * count
price ← 4
PRINT total
Guess first: is 15 or 12 printed?
It prints 15. total is calculated on line 3, and it stores the result, not the formula. price changes on line 4, but that does not change total.
Meaning: To check a program like this, run it yourself on paper, line by line, and write what the variables hold in a trace table. A trace table has one column for each variable and one output column. It gets one more row every time a variable changes or something is printed. In the Playground, the Trace table tab makes this table for you.
⚡ Write down the condition checks too
Write:
READ n
total ← 0
day ← 1
WHILE day < n DO
READ sale
total ← total + sale
day ← day + 1
END WHILE
PRINT "Total:", total
Guess first: this program adds up the sales of n days, and n holds 3. How many days of sales are read?
Only two. Once day becomes 3, the condition day < n becomes 3 < 3, and that is FALSE. The loop stops before the third day's sales are read.
Meaning: A mistake in a program like this one is called a bug. Checking a condition does not change a variable, so it does not show in the trace table, but the bug is in that check. Write the result of each check beside the table: 1 < 3 TRUE, 2 < 3 TRUE, 3 < 3 FALSE.
⚡ Off by one
Meaning: The bug in the previous step is called off-by-one: the loop runs one round too few, or one round too many. The cause is almost always a < mixed up with <=, or a start of 0 mixed up with 1. With WHILE day <= n, all three days are read. For sales of 120, 95 and 140, the result is Total: 355.
⚡ PRINT as a trace
Meaning: For a long program, add a line like PRINT "trace:", day, sale, total where you suspect the problem is. That line prints what the variables hold in each round, so the computer keeps the record. Delete the line again once the bug is found.