A trace table is a technique used to test algorithms or computer programs for logic errors that occur while the algorithm or program executes.
The trace table simulates the flow of execution. Statements are executed step by step, and the values of variables change as an assignment statement is executed.
Trace tables are typically used by novice programmers to help them visualize how a particular algorithm or program works. Trace tables can also help advanced programmers detect logic errors.
A typical trace table is shown here.
Step | Statement | Notes | variable1 | variable2 | variable3 | … |
1 | ||||||
2 | ||||||
3 | ||||||
… |
Let’s see a trace table in action! For the following program, a trace table is created to determine the values of the variables in each step.
PHP
<?php
$x = 10;
$y = 15;
$z = $x * $y;
$z++;
echo $z;
?>
The trace table for this program is shown below. Notes are optional, but they help the reader to better understand what is really happening.
Step | Statement | Notes | $x | $y | $z |
1 | $x = 10 | The value 10 is assigned to variable $x . | 10 | ? | ? |
2 | $y = 15 | The value 15 is assigned to variable $y . | 10 | 15 | ? |
3 | $z = $x * $y | The result of the product $x * $y is assigned to $z . | 10 | 15 | 150 |
4 | $z++ | Variable $z is incremented by one. | 10 | 15 | 151 |
5 | echo $z | The value 151 is displayed. |
Java
public static void main(String[] args) throws java.io.IOException {
int x, y, z;
x = 10;
y = 15;
z = x * y;
z++;
System.out.println(z);
}
The trace table for this program is shown below. Notes are optional, but they help the reader to better understand what is really happening.
Step | Statement | Notes | x | y | z |
1 | x = 10 | The value 10 is assigned to variable x . | 10 | ? | ? |
2 | y = 15 | The value 15 is assigned to variable y . | 10 | 15 | ? |
3 | z = x * y | The result of the product x * y is assigned to z . | 10 | 15 | 150 |
4 | z++ | Variable z is incremented by one. | 10 | 15 | 151 |
5 | System.out.println(d) | The value 151 is displayed. |
C++
#include <iostream>
using namespace std;
int main() {
int x, y, z;
x = 10;
y = 15;
z = x * y;
z++;
cout << z;
return 0;
}
The trace table for this program is shown below. Notes are optional, but they help the reader to better understand what is really happening.
Step | Statement | Notes | x | y | z |
1 | x = 10 | The value 10 is assigned to variable x . | 10 | ? | ? |
2 | y = 15 | The value 15 is assigned to variable y . | 10 | 15 | ? |
3 | z = x * y | The result of the product x * y is assigned to z . | 10 | 15 | 150 |
4 | z++ | Variable z is incremented by one. | 10 | 15 | 151 |
5 | cout << z | The value 151 is displayed. |
C#
static void Main() {
int x, y, z;
x = 10;
y = 15;
z = x * y;
z++;
Console.Write(z);
Console.ReadKey();
}
The trace table for this program is shown below. Notes are optional, but they help the reader to better understand what is really happening.
Step | Statement | Notes | x | y | z |
1 | x = 10 | The value 10 is assigned to variable x . | 10 | ? | ? |
2 | y = 15 | The value 15 is assigned to variable y . | 10 | 15 | ? |
3 | z = x * y | The result of the product x * y is assigned to z . | 10 | 15 | 150 |
4 | z++ | Variable z is incremented by one. | 10 | 15 | 151 |
5 | Console.Write(z) | The value 151 is displayed. |
Visual Basic
Sub Main()
Dim x, y, z As Integer
x = 10
y = 15
z = x * y
z += 1
Console.Write(z)
Console.ReadKey()
End Sub
The trace table for this program is shown below. Notes are optional, but they help the reader to better understand what is really happening.
Step | Statement | Notes | x | y | z |
1 | x = 10 | The value 10 is assigned to variable x . | 10 | ? | ? |
2 | y = 15 | The value 15 is assigned to variable y . | 10 | 15 | ? |
3 | z = x * y | The result of the product x * y is assigned to z . | 10 | 15 | 150 |
4 | z += 1 | Variable z is incremented by one. | 10 | 15 | 151 |
5 | Console.Write(z) | The value 151 is displayed. |
Python
x = 10
y = 15
z = x * y
z += 1
print(z)
The trace table for this program is shown below. Notes are optional, but they help the reader to better understand what is really happening.
Step | Statement | Notes | x | y | z |
1 | x = 10 | The value 10 is assigned to variable x . | 10 | ? | ? |
2 | y = 15 | The value 15 is assigned to variable y . | 10 | 15 | ? |
3 | z = x * y | The result of the product x * y is assigned to z . | 10 | 15 | 150 |
4 | z += 1 | Variable z is incremented by one. | 10 | 15 | 151 |
5 | print(z) | The value 151 is displayed. |