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 TotalTrace table
| Count | Total | OUTPUT |
|---|---|---|
| 1 | 1 | — |
| 2 | 3 | — |
| 3 | 6 | — |
| — | — | 6 |
After the loop finishes, Total is 6, which is what the OUTPUT statement sends to the screen.
Concepts — What is a Trace Table?
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 TotalTrace table
| Count | Total | OUTPUT |
|---|---|---|
| 1 | 1 | — |
| 2 | 3 | — |
| 3 | 6 | — |
| — | — | 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.
Creating a Trace Table
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 GradeWalk-through (Mark = 65)
| Step | Condition tested | Result | Grade | OUTPUT |
|---|---|---|---|---|
| 1 | Mark >= 80 | FALSE | — | — |
| 2 | Mark >= 60 | TRUE | B | — |
| 3 | — (OUTPUT runs) | — | B | B |
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 >.
Tracing Conditional Statements (IF)
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 SumTrace table
| Num | Sum | OUTPUT |
|---|---|---|
| 1 | 1 | — |
| 2 | 3 | — |
| 3 | 6 | — |
| 4 | — | 6 |
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.
Tracing Loops
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 TotalTrace table
| I | Numbers[I] | Total |
|---|---|---|
| 1 | 5 | 5 |
| 2 | 10 | 15 |
| 3 | 15 | 30 |
| 4 | 20 | 50 |
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.
Tracing Arrays
18.6 Key Points Summary
A quick recap of everything covered on trace tables:
- Tracks variable values — A trace table records the value of every variable after each step of the algorithm.
- One row per iteration — For loops, each row is one iteration. For sequential code, each row is one statement.
- Include ALL variables — Every variable (plus OUTPUT) gets its own column — never miss one out.
- OUTPUT column — Always include an OUTPUT column. Fill it only on the row where OUTPUT actually runs.
- Trace actual execution — Record what ACTUALLY happens — never what you think should happen.
- Used for debugging — Trace tables expose logic errors by showing the exact step where a variable goes wrong.
- IF statements — Record the condition result (TRUE/FALSE) and which branch was taken at each level.
- Loops — Record the counter and accumulated values per iteration. Add a final row showing the value that fails the condition.
- Arrays — Include the index and the element value being accessed — e.g. columns I and Numbers[I].
- Dry-running — A 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.