Trace Tables
Completing a trace table means carrying on the record of an algorithm's run from the values already written down, filling in the rest one line at a time. Done carefully, it tells you exactly what the algorithm outputs and where a logic error hides.
Every variable set before the first loop or selection gets its own row, in the order the algorithm sets it. If the table has a line-number column, a line that only assigns a value still gets a row to itself, so the order of events stays clear.
A partly filled table is always continued, never restarted. If the last completed row shows 3 in the turns column and 12 in the credits column, the next row you write works from those two values.
For example, once the variable skip holds 4 and limit holds 6, testing whether skip is less than limit records True in that row.
A fairground ride uses 5 credits a go, and the programmer wants to count how many goes were taken.
credits = 15
turns = 0
reply = input('Go again? ')
while reply == 'yes':
credits = credits - 5
turns = turns + 1
reply = input('Go again? ')
print(turns)Output when the user types yes, yes, no:
Go again? yes
Go again? yes
Go again? no
2The first six rows below were supplied, ending with 10 in the credits column and 1 in the turns column. The second yes was typed next, so carrying on from those values finishes the table.
| reply | credits | turns | reply == 'yes' | output |
|---|---|---|---|---|
| 15 | ||||
| 0 | ||||
| yes | ||||
| True | ||||
| 10 | ||||
| 1 | ||||
| yes | ||||
| True | ||||
| 5 | ||||
| 2 | ||||
| no | ||||
| False | ||||
| 2 |
OCR GCSE Computer Science · Trace Tables