±«Óătv

Trace tables

One way to test short programs is to do what is known as a dry run using paper. A dry run involves creating what is called a trace table, containing all the variables a contains. Whenever the value of a variable changes, the change is indicated in the trace table.

Consider this simple program:

1   total = 0
2   for count = 1 to 3
3       number = int(input("Enter number ", count))
4       total = total + number
5  next count

Each has been given a line number (1-5). The program has three variables - total, count and number – which will be put into a trace table.

Instructiontotalcountnumber
Instruction
total
count
number

Next, the program is tested using test data. If the numbers 5, 7 and 9 are inputted, the resulting total should be 21.

The instruction number is inputted. If a variable changes with that instruction, the new variable value is written in the appropriate box, as follows:

Instructiontotalcountnumber
10
21
35
45
5
22
37
412
5
23
39
421
5
Instruction1
total0
count
number
Instruction2
total
count1
number
Instruction3
total
count
number5
Instruction4
total5
count
number
Instruction5
total
count
number
Instruction2
total
count2
number
Instruction3
total
count
number7
Instruction4
total12
count
number
Instruction5
total
count
number
Instruction2
total
count3
number
Instruction3
total
count
number9
Instruction4
total21
count
number
Instruction5
total
count
number

At each step, the programmer is able to see if, and how, a variable is affected.

Trace tables are extremely useful because they enable a programmer to compare what the value of each variable should be against what a program actually produces. Where the two differ is the point in the program where a logic error has occurred.