Trace Tables
A dry run means following an algorithm by hand, step by step, without running it on a computer, keeping track of the value held in every variable. A trace table records the dry run: each row shows the values reached at one step, so you can see exactly how the algorithm behaves.
Trace tables show what an algorithm does, what it outputs for given inputs, how many times a loop repeats, and where a logic error sits โ a mistake in the algorithm's reasoning, which appears as a value you did not expect. They cannot help with syntax errors, because a program with one of those never runs. Follow the actual lines in order and write down what really happens, not what the program is meant to do.
word = input("Enter a word: ")
for letter in word:
print(letter)
if letter == "E":
print("Cheer!")Output when the user types BEES:
Enter a word: BEES
B
E
Cheer!
E
Cheer!
S
Here the input BEES arrives in word, and each pass of the loop adds a row showing the letter and the result of the test:
| word | letter | letter == 'E' | Output |
|---|---|---|---|
| BEES | |||
| B | False | B | |
| E | True | E, then Cheer! | |
| E | True | E, then Cheer! | |
| S | False | S |
The loop repeats four times, once per letter, yet six lines are printed, because each true test prints a second line. The number of iterations and the number of output lines are not the same thing.
OCR GCSE Computer Science ยท Trace Tables