How to Trace Code by Hand Without Losing Your Place

Learning how to trace code by hand starts with giving yourself a reliable place to look when you forget what just happened. In a nested loop, it is easy to remember that a total is now 4 but forget which pair of loop values produced it. A trace table keeps those facts together. Each row records one step you chose to follow, the values at that step, and anything the program printed.
How to trace code by hand with a table
Read the question before drawing the table. If it asks what the program prints, give output its own column. If it asks for a variable's final value, make sure that variable has a column even if it never gets printed. Include loop variables and any values used in a condition. A step column gives you a place to label initialization, loop iterations, and statements that run after a loop.
- Step: Identify the event the row represents, such as an inner loop iteration or a print statement after the loops.
- Variables: Use one column per variable. If an assignment is easy to misread, record its old and new values separately.
- Branch decision: Record the result of the condition, such as
TrueorFalse. Use a blank or a label like not tested when no condition was evaluated. - Output: Write only what was printed during that row. Mark a silent row with none so you do not mistake an empty cell for unfinished work.
Choose what a row means before filling in numbers. For the example below, each loop row covers one full pass through the inner loop body. The table records the total both before and after that pass, so there is no guesswork about which value the program uses.
Start a new row on each inner pass
This Python example uses i for the outer loop and j for the inner loop. The outer loop takes the values 1 and 2. For each value of i, the inner loop starts again and takes the values 1, 2, and 3. The stop value in a Python range is excluded, as shown in the Python tutorial on control flow.
total = 0
for i in range(1, 3):
for j in range(1, 4):
if (i + j) % 2 == 0:
total = total + j
print(total)
print(total)
The expression (i + j) % 2 gives the remainder when the sum is divided by 2. A remainder of zero means the sum is even. Only then does the program add j to total and print the updated value.
| Step | i | j | Total before | Sum even? | Total after | Output |
|---|---|---|---|---|---|---|
| Initialize | unset | unset | unset | not tested | 0 | none |
| Inner pass 1 | 1 | 1 | 0 | True | 1 | 1 |
| Inner pass 2 | 1 | 2 | 1 | False | 1 | none |
| Inner pass 3 | 1 | 3 | 1 | True | 4 | 4 |
| Inner pass 4 | 2 | 1 | 4 | False | 4 | none |
| Inner pass 5 | 2 | 2 | 4 | True | 6 | 6 |
| Inner pass 6 | 2 | 3 | 6 | False | 6 | none |
| After both loops | 2 | 3 | 6 | not tested | 6 | 6 |
Look at the move from pass 3 to pass 4. There is no next value of j in the current inner loop. The outer loop advances i to 2, and the inner loop starts at j = 1 again. Start a new row at that pair. The total carries forward as 4 because total = 0 sits outside both loops.
A false branch still gets a row. Pass 4 checks 2 + 1, skips the two indented statements under the if, and leaves the total at 4. The last row is different: it is not a loop pass at all. The final print(total) is outside both loops, so it prints 6 again. Read the output column from top to bottom: 1, 4, 6, 6, each on its own line.
Keep your place when the code changes direction
Fill each row from left to right. Write the loop values before doing any arithmetic. Copy the previous row's ending total into the new row's starting column, then evaluate the condition. If the condition is true, follow the indented body in order. If it is false, copy the starting total to the ending column and record no output. Do not advance either loop variable until the row is complete.
On paper, put a small mark beside the statement you are about to execute. Move that mark as you go. This helps when two lines refer to the same variable: total = total + j first reads the old total and then replaces it, while the next line reads the new total. If several assignments in a longer loop make one row hard to follow, switch to one row per executed statement and add a line-number column.
Check the trace before writing your answer
Check the order of the loop pairs before checking the arithmetic. In this example, each outer value should have three inner rows in the order 1, 2, 3. Next, compare each row's starting total with the previous row's ending total. Finally, match every output cell to a print statement that actually ran. These checks catch different mistakes: a missing pass, an accidental reset, or a number copied from a variable even though nothing printed it.
For another practice problem, try the ICSC training and practice problems or browse the Exam Preparation articles. To practice on this example again, move total = 0 just inside the outer loop, before the inner loop. Give each reset its own row, then compare the two traces at the moment i becomes 2.
Frequently asked questions
Do I need a row for every line of code?
No. One row per loop pass works when you can follow the statements in that pass without confusion. Use one row per executed statement when the order of several updates is hard to track.
Do I skip a row when the condition is false?
No. Record the false decision and carry forward any values that did not change. If the skipped branch contains the only print statement in that pass, record no output.
Why does the example print 6 twice?
The branch prints 6 when i and j are both 2. The print statement after both loops runs separately and prints the same total again.



