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.

StepStatementNotesvariable1variable2variable3
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.

StepStatementNotes$x$y$z
1$x = 10The value 10 is assigned to variable $x.10??
2$y = 15The value 15 is assigned to variable $y.1015?
3$z = $x * $yThe result of the product $x * $y is assigned to $z.1015150
4$z++Variable $z is incremented by one.1015151
5echo $zThe 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.

StepStatementNotesxyz
1x = 10The value 10 is assigned to variable x.10??
2y = 15The value 15 is assigned to variable y.1015?
3z = x * yThe result of the product x * y is assigned to z.1015150
4z++Variable z is incremented by one.1015151
5System.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.

StepStatementNotesxyz
1x = 10The value 10 is assigned to variable x.10??
2y = 15The value 15 is assigned to variable y.1015?
3z = x * yThe result of the product x * y is assigned to z.1015150
4z++Variable z is incremented by one.1015151
5cout << zThe 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.

StepStatementNotesxyz
1x = 10The value 10 is assigned to variable x.10??
2y = 15The value 15 is assigned to variable y.1015?
3z = x * yThe result of the product x * y is assigned to z.1015150
4z++Variable z is incremented by one.1015151
5Console.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.

StepStatementNotesxyz
1x = 10The value 10 is assigned to variable x.10??
2y = 15The value 15 is assigned to variable y.1015?
3z = x * yThe result of the product x * y is assigned to z.1015150
4z += 1Variable z is incremented by one.1015151
5Console.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.

StepStatementNotesxyz
1x = 10The value 10 is assigned to variable x.10??
2y = 15The value 15 is assigned to variable y.1015?
3z = x * yThe result of the product x * y is assigned to z.1015150
4z += 1Variable z is incremented by one.1015151
5print(z)The value 151 is displayed.