Tracing Code by Hand: How Dry Runs and Trace Tables Prevent Bugs
Discover how to step through your code line by line on paper using a trace table to spot off-by-one errors and logic bugs before you even hit Run.
In This Article
- Understand what it means to perform a 'dry run' by acting as the computer and following instructions literally.
- Construct a structured trace table tracking step numbers, variables, condition checks, and output.
- Walk through loops containing conditional statements without losing track of changing variable values.
- Catch common mistakes like off-by-one errors and uninitialized or stale variables early.
Stop Guessing and Start "Playing Computer"
When a program produces the wrong answer, a common beginner habit is to stare at the screen, change a < to a <= or add + 1 somewhere at random, and click Run again to see if that fixed it. Computer scientists call this "shotgun debugging"—making guesses until the output happens to look right. Not only is guessing frustrating and slow, it often hides a deeper flaw that will break your code on the very next level or test case.
The antidote to guessing is tracing code by hand, also called performing a dry run. In a dry run, you temporarily set aside what you wished the code would do, and instead act as the CPU executing the instructions literally, one line or block at a time. Because human short-term memory can only hold a few changing numbers at once, programmers record each step of a dry run in a grid called a trace table.
How to Set Up a Trace Table
You can draw a trace table on scrap paper, a whiteboard, or a simple text file. A clear trace table usually has five types of columns from left to right:
- Step: A sequential counter (1, 2, 3...) showing the order in which operations happen.
- Location (Line or Block): Which instruction is currently executing.
- Variable Columns (one column per variable): Every variable in your program gets its own column. Whenever an assignment changes a variable, write its new value in that row.
- Condition Result: Whenever an
ifstatement or loop header evaluates a boolean condition, writeTrueorFalsehere so you can see why a branch was taken or skipped. - Output: Anything printed to the screen or performed in the game world during that step.
Step-by-Step Walkthrough: Tracing a Loop with an If Condition
Let us trace a short program that processes a list of energy cells and adds up only the cells that have an even charge greater than 10. First, inspect the code in visual blocks, Python, and C++:
Visual Block Logic
1: SET cells TO [8, 14, 15, 20]
2: SET usable_energy TO 0
3: FOR EACH cell IN cells DO
4: IF cell > 10 AND (cell MOD 2 == 0) THEN
5: CHANGE usable_energy BY cell
6: END IF
7: END FOR
8: PRINT usable_energy
Python
cells = [8, 14, 15, 20]
usable_energy = 0
for cell in cells:
if cell > 10 and cell % 2 == 0:
usable_energy += cell
print(usable_energy)
C++
#include <iostream>
#include <vector>
int main() {
std::vector<int> cells = {8, 14, 15, 20};
int usable_energy = 0;
for (int cell : cells) {
if (cell > 10 && cell % 2 == 0) {
usable_energy += cell;
}
}
std::cout << usable_energy << "
";
return 0;
}
Now let us walk through every single step of this execution as if we were the processor:
- Step 1 (Line 1–2): Initialize
cells = [8, 14, 15, 20]andusable_energy = 0. No loop iteration has started yet. - Step 2 (Line 3 — Iteration 1): The loop grabs the first item (index
0). Nowcell = 8, whileusable_energyis still0. - Step 3 (Line 4): Evaluate the condition
cell > 10 and cell % 2 == 0forcell = 8. Is8 > 10?False. Because anANDexpression requires both sides to be true, the whole condition evaluates to False. Line 5 is skipped! - Step 4 (Line 3 — Iteration 2): The loop advances to the second item (index
1). Nowcell = 14, andusable_energy = 0. - Step 5 (Line 4): Evaluate
14 > 10(True) and14 % 2 == 0(True, since 14 divided by 2 leaves remainder 0). Condition result: True. - Step 6 (Line 5): Execute the body of the
ifstatement:usable_energybecomes0 + 14 = 14. - Step 7 (Line 3 — Iteration 3): The loop advances to the third item (index
2). Nowcell = 15, andusable_energy = 14. - Step 8 (Line 4): Evaluate
15 > 10(True) and15 % 2 == 0(False, since 15 is odd and leaves remainder 1). Condition result: False. Line 5 is skipped, sousable_energyremains14. - Step 9 (Line 3 — Iteration 4): The loop advances to the fourth item (index
3). Nowcell = 20, andusable_energy = 14. - Step 10 (Line 4): Evaluate
20 > 10(True) and20 % 2 == 0(True). Condition result: True. - Step 11 (Line 5): Update
usable_energyto14 + 20 = 34. - Step 12 (Line 8): The loop finishes because there are no more elements in
cells. Line 8 prints the final output:34.
How Tracing Exposes Classic Bugs Immediately
Tracing is especially powerful for catching two notorious bug categories that look harmless at a glance:
1. Off-by-One Errors
Suppose you want to move a character forward 5 tiles using a while loop: steps = 1 and while steps < 5: move_forward(); steps += 1. When you trace this on paper, you write down the rows where the loop runs: steps = 1 (move 1), steps = 2 (move 2), steps = 3 (move 3), steps = 4 (move 4). Then steps becomes 5, the condition 5 < 5 evaluates to False, and the loop stops! Your trace table immediately shows only four moves happened instead of five.
2. Stale Variables Inside Loops
Another frequent mistake happens when a programmer accidentally puts an initialization line inside a loop instead of before the loop—or vice versa. For example, if you placed usable_energy = 0 inside the for cell in cells: loop, your trace table would show usable_energy resetting back to 0 at the top of every single iteration, immediately revealing why only the final item was ever counted.
Try It Yourself
Challenge: Complete a Dry Run Trace Table
Grab a piece of paper or open a notes file and trace the following code by hand without running it on a computer:
x = 2
total = 0
while x <= 8:
if x != 4:
total = total + x
x = x + 2
print(total)
- Write down the values of
x, the condition checkx <= 8, the inner checkx != 4, andtotalfor each pass of the loop. - How many times does the condition
x <= 8evaluate toTrue? - What is the exact value of
xat the moment the loop terminates? - What final number does
print(total)output? (Check your trace: you should get16from adding2 + 6 + 8, whilexends at10!)
CodeCracker