±«Óătv

Trace tables and algorithm dry run

After code has been written, it is vital it undergoes testing. Without testing, there is a chance the code will not work.

Trace tables

When designing , it is common to use a technique known as .

To create a trace table, map out all of the variables which change (not constants) and write them down in a column in a table. Each row will then store what assignments happen as the code is run. Reading through code and noting down values in a trace table is known as a .

Consider the following code to find even numbers between 0 and 3:

1 for i in range (0, 3):
2     if i % 2 == 0:
3         print(i)
4     i += 1

The following Trace Table could be used to dry run the code:

Program Line NumberValue of variable iOutput
10-
20-
300
41-
11-
21-
31-
42-
12-
22-
322
43-
13-
23-
33-
34-
Program Line Number1
Value of variable i0
Output-
Program Line Number2
Value of variable i0
Output-
Program Line Number3
Value of variable i0
Output0
Program Line Number4
Value of variable i1
Output-
Program Line Number1
Value of variable i1
Output-
Program Line Number2
Value of variable i1
Output-
Program Line Number3
Value of variable i1
Output-
Program Line Number4
Value of variable i2
Output-
Program Line Number1
Value of variable i2
Output-
Program Line Number2
Value of variable i2
Output-
Program Line Number3
Value of variable i2
Output2
Program Line Number4
Value of variable i3
Output-
Program Line Number1
Value of variable i3
Output-
Program Line Number2
Value of variable i3
Output-
Program Line Number3
Value of variable i3
Output-
Program Line Number3
Value of variable i4
Output-