Validation & Testing

Trace Tables

A trace table tracks the value of every variable as an algorithm runs line by line. It is the standard tool for dry-running pseudocode and finding logic errors before you ever touch a keyboard.

18.1 What is a Trace Table?

A trace table is a manual tool used to track the values of variables as an algorithm executes step by step. Each row of the table shows the state of every variable after one step of the algorithm has run.

  • Each row represents one iteration (for a loop) or one statement (for sequential code).
  • Each column represents one variable, plus an OUTPUT column.
  • Used for debugging, understanding algorithms, dry-running code, and exam questions.
  • Helps identify logic errors by showing exactly what happens at each step.

Key idea: a trace table is a paper-based simulation of the computer. You become the CPU — reading each line, updating variables, and recording the new state row by row.

Here is a simple counting loop and the trace table that results from dry-running it:

DECLARE Count : INTEGER
DECLARE Total : INTEGER
Total <- 0
FOR Count <- 1 TO 3
  Total <- Total + Count
NEXT Count
OUTPUT Total

Trace table

CountTotalOUTPUT
11
23
36
6

After the loop finishes, Total is 6, which is what the OUTPUT statement sends to the screen.

18.2 Creating a Trace Table

Building a trace table is a four-step process. Follow these steps in order every time.

  • Step 1 — Identify all variables in the algorithm.
  • Step 2 — Create a column for each variable (and an OUTPUT column).
  • Step 3 — Execute the algorithm line by line, on paper.
  • Step 4 — Record variable values after each line executes.

Worked example — a simple counting loop:

DECLARE Count : INTEGER
DECLARE Total : INTEGER
Total <- 0
FOR Count <- 1 TO 3
  Total <- Total + Count
NEXT Count
OUTPUT Total

Trace table

CountTotalOUTPUT
11
23
36
6
  • Iteration 1: Total <- 0 + 1 = 1.
  • Iteration 2: Total <- 1 + 2 = 3.
  • Iteration 3: Total <- 3 + 3 = 6.
  • After the loop: OUTPUT 6.

Exam tip: always include an OUTPUT column even if you do not think you will use it. Examiners expect to see when (and what) the algorithm outputs — leaving the column out loses easy marks.

18.3 Tracing Conditional Statements (IF)

When you trace an IF statement, you record which branch was taken and the values assigned in that branch. Nested IFs are traced the same way — just one level at a time.

  • Evaluate the condition — is it TRUE or FALSE?
  • Take the matching branch — THEN if TRUE, ELSE if FALSE.
  • Record any variable changes that happen inside that branch.
  • For nested IFs, repeat the same process at each level.

Example — a grade calculator with nested IFs:

DECLARE Mark : INTEGER
DECLARE Grade : STRING
Mark <- 65
IF Mark >= 80 THEN
  Grade <- "A"
ELSE
  IF Mark >= 60 THEN
    Grade <- "B"
  ELSE
    Grade <- "C"
  ENDIF
ENDIF
OUTPUT Grade

Walk-through (Mark = 65)

StepCondition testedResultGradeOUTPUT
1Mark >= 80FALSE
2Mark >= 60TRUEB
3— (OUTPUT runs)BB

Mark = 65 — first test (65 >= 80) is FALSE so we go to ELSE. Second test (65 >= 60) is TRUE so Grade <- "B". The OUTPUT then sends "B" to the screen.

Rule of thumb: for each IF, write down the condition result (TRUE/FALSE) before recording the new variable values. That single habit catches most logic errors — including >= written as >.

18.4 Tracing Loops

For loops, each row in the trace table corresponds to one iteration of the loop body. Record the counter variable, any accumulated values, and any output produced.

  • One row per iteration of the loop body.
  • Record the counter variable at the start of each iteration.
  • Record accumulated values (sums, products, etc.) after each update.
  • OUTPUT row appears last, when the OUTPUT statement runs.

Example — a WHILE loop that sums the numbers 1 to 3:

DECLARE Num : INTEGER
DECLARE Sum : INTEGER
Sum <- 0
Num <- 1
WHILE Num <= 3 DO
  Sum <- Sum + Num
  Num <- Num + 1
ENDWHILE
OUTPUT Sum

Trace table

NumSumOUTPUT
11
23
36
46

The last row (Num = 4) shows the value that FAILS the condition — at that point the loop exits and the OUTPUT statement runs.

Common mistake: forgetting the final row that shows the counter at exit. After iteration 3, Num becomes 4 — and that value is what makes Num <= 3 FALSE. Always add this row so the trace tells the full story.

18.5 Tracing Arrays

When tracing array operations (storing, accessing, or searching), include the array index in the trace so it is obvious which element was used on each iteration.

  • Include the index variable (e.g. I) as a column.
  • Include a column for the element value (e.g. Numbers[I]).
  • One row per iteration, just like a normal loop trace.
  • You do not need a column for every element — just the one being accessed.

Example — finding the sum of an array:

DECLARE Numbers : ARRAY[1:4] OF INTEGER
DECLARE I : INTEGER
DECLARE Total : INTEGER
Numbers[1] <- 5
Numbers[2] <- 10
Numbers[3] <- 15
Numbers[4] <- 20
Total <- 0
FOR I <- 1 TO 4
  Total <- Total + Numbers[I]
NEXT I
OUTPUT Total

Trace table

INumbers[I]Total
155
21015
31530
42050

After the loop finishes, Total = 50 — the OUTPUT statement sends 50 to the screen.

Key idea: the Numbers[I] column shows the VALUE retrieved from the array on that iteration. It is much clearer than listing every array element — and it tells you exactly which element was used.

18.6 Key Points Summary

A quick recap of everything covered on trace tables:

  • Tracks variable valuesA trace table records the value of every variable after each step of the algorithm.
  • One row per iterationFor loops, each row is one iteration. For sequential code, each row is one statement.
  • Include ALL variablesEvery variable (plus OUTPUT) gets its own column — never miss one out.
  • OUTPUT columnAlways include an OUTPUT column. Fill it only on the row where OUTPUT actually runs.
  • Trace actual executionRecord what ACTUALLY happens — never what you think should happen.
  • Used for debuggingTrace tables expose logic errors by showing the exact step where a variable goes wrong.
  • IF statementsRecord the condition result (TRUE/FALSE) and which branch was taken at each level.
  • LoopsRecord the counter and accumulated values per iteration. Add a final row showing the value that fails the condition.
  • ArraysInclude the index and the element value being accessed — e.g. columns I and Numbers[I].
  • Dry-runningA trace table lets you dry-run pseudocode by hand, with no computer needed.

Exam tip: in a trace-table exam question, set up the column headings FIRST (one per variable + OUTPUT), then fill in rows one at a time. Always add the final "loop exit" row showing the value that fails the condition — examiners look for it.

Question Bank

Answer all questions, then press Submit Quiz to see your score.

0/12 answered

Question 1Multiple Choice

What is the main purpose of a trace table?

Question 2True / False

A trace table has one row per iteration of a loop.

Question 3Multiple Choice

What is the OUTPUT of this code?

DECLARE Total : INTEGER
DECLARE I : INTEGER
Total <- 0
FOR I <- 1 TO 4
  Total <- Total + I
NEXT I
OUTPUT Total

Question 4Multiple Choice

Which column should EVERY trace table include alongside the variables?

Question 5True / False

A trace table can only be used with loops, not with IF statements.

Question 6Multiple Choice

What is the final value of X?

DECLARE X : INTEGER
X <- 2
WHILE X < 20 DO
  X <- X * 2
ENDWHILE
OUTPUT X

Question 7True / False

When tracing an IF statement, you should record the result of the condition (TRUE or FALSE).

Question 8Multiple Choice

What is the final value of Total?

DECLARE Count : INTEGER
DECLARE Total : INTEGER
Total <- 0
FOR Count <- 1 TO 4
  Total <- Total + Count * 2
NEXT Count
OUTPUT Total

Question 9Multiple Choice

A trace table is MOST helpful for finding which type of error?

Question 10True / False

An empty cell in a trace table means the variable's value has not yet been set or has not changed.

Question 11Multiple Choice

When tracing WHILE Num <= 3 where Num starts at 1 and increments by 1 each iteration, how many iteration rows appear in the trace?

Question 12Multiple Choice

How many times will OUTPUT execute?

DECLARE Count : INTEGER
Count <- 0
WHILE Count < 5 DO
  OUTPUT Count
  Count <- Count + 1
ENDWHILE

Answer all 12 questions to enable submission.