Services Available in UAE, KSA, Qatar, India and Bahrain.

How to Trace Code by Hand

How to Trace Code by Hand

How to Trace Code by Hand: Variables, Loops and Conditions

A program can look familiar while still producing an unexpected result. You recognise the loop and understand the individual statements, but lose track of how the values change.

Tracing code helps you slow down and follow execution one step at a time.

For school Computer Science students, it is a useful way to understand programs, investigate mistakes and prepare for questions that ask for the output of an algorithm.

What Does Tracing Code Mean?

Tracing means following the order in which a program executes and recording changes to its variables.

You do not guess what the code is intended to do. You follow what its instructions actually say.

A trace table gives each important variable a column. Each row records a chosen point in execution, such as the end of a loop iteration.

Always state what each row represents so the table stays consistent.

Start With a Simple Variable Update

Consider this Python code:

score = 5
score = score + 3
print(score)

The first statement stores 5 in score.

The second statement reads the current value, adds 3 and stores the result back in the same variable.

The program prints:

8

In this context, = is an assignment operator. The statement updates a stored value; it is not a mathematical claim that score equals score + 3.

Trace a Loop With a Table

Now consider:

total = 0

for number in [2, 4, 6]:
    total = total + number

print(total)

Record the value of total after each iteration:

StageCurrent numberTotal after the update
Before the loop—0
First iteration22
Second iteration46
Third iteration612

The program prints 12.

Notice that total keeps its updated value between iterations. It does not return to zero unless the program explicitly resets it.

Check Where Initialisation Happens

Move the initialisation inside the loop:

for number in [2, 4, 6]:
    total = 0
    total = total + number

print(total)

Now total becomes zero at the start of every iteration.

After the final iteration, its value is 6, so the program prints 6.

The change is small, but the behaviour is different. When tracing, pay attention to indentation and where variables are initialised.

Add a Condition

Consider:

count = 0

for number in [3, 8, 5, 10]:
    if number > 5:
        count = count + 1

print(count)

The variable changes only when the condition is true:

NumberIs number greater than 5?Count after the iteration
3No0
8Yes1
5No1
10Yes2

The output is 2.

The value 5 does not qualify because the condition is > 5, not >= 5. A trace table makes this boundary visible.

Track Output Separately From Variables

The position of print() also matters.

total = 0

for number in [2, 4, 6]:
    total = total + number
    print(total)

This prints after every update:

2
6
12

When print(total) is outside the loop, only the final total is printed.

Add an output column to your trace table when a program contains several output statements.

Common Tracing Mistakes

Students often make errors when they:

  • Use a variable’s old value after it has changed.

  • Skip an iteration.

  • Execute a conditional statement even when its condition is false.

  • Confuse a list index with the value stored at that index.

  • Miss a variable reset inside a loop.

  • Forget that Python’s range() excludes its stop value.

For example, range(1, 4) produces 1, 2 and 3—not 4.

Try This Before Running It

Predict the output:

result = 1

for number in [2, 3, 4]:
    result = result * number

print(result)

Write the starting value and update it once per iteration.

The values are:

1 → 2 → 6 → 24

The final output is 24.

Now consider what would happen if result started at zero. Every multiplication would leave it at zero. The initial value is part of the algorithm’s behaviour.

Use Tracing to Improve Your Own Programs

Before running a short program, predict what it will do. Then compare your prediction with the actual output.

If they differ, locate the first step where your expectation and the program’s behaviour separate.

For personalised support with programming and school Computer Science, book a free demo with TheTuitionE. Share your curriculum, programming language and an example you find difficult.

The goal is to explain how the program reaches its result, one instruction at a time.

Last Updated At: 2026-10-25 22:27:33

Check out these Related Articles

Book Free Demo