Skip to content
Padhnus

Programming fundamentals

Trace a loop before you run it

Follow variable values through a Python loop, diagnose a boundary mistake, and check your predictions.

6 min read · Updated

What you’ll learn

  • Track variable values one iteration at a time.
  • Explain an excluded range endpoint and a loop that runs zero times.

Before you start: You should recognize assignment, comparison and the remainder operator (%) in Python 3.

In this guide 7 sections

A trace table records what a program does after each step. It is useful when the code runs without an error but produces the wrong answer. This guide uses Python 3; other languages may express loops differently. You need to recognize assignment, addition and an if condition.

Predict first, then check

Read this program without running it. Write down the numbers visited by the loop and predict the final value of total.

total = 0
for number in range(1, 6):
    if number % 2 == 0:
        total = total + number
print(total)

range(1, 6) produces 1, 2, 3, 4 and 5: the stopping value 6 is excluded. The remainder expression number % 2 is zero for these even numbers. Only iterations that pass the condition change total.

One row per iteration
numberEven?total beforetotal after
1No00
2Yes02
3No22
4Yes26
5No66

The final output is 6. Notice that a skipped addition does not reset the total. The previous value carries into the next iteration.

Find the boundary mistake

Suppose the task actually asks for the sum of even integers from 1 through 6. The original program omits 6. Changing the stop to range(1, 7) includes it, producing 2 + 4 + 6 = 12.

Test the boundary with a very small case. Through 2, the answer should be 2. If your program reports zero, check whether the loop reaches 2. This small example makes an off-by-one mistake easier to see than a run through a thousand values.

Check a loop that runs zero times

With the default positive step, the starting value must be below the stopping value. Predict this output before opening the explanation:

total = 10
for number in range(4, 4):
    total = total + number
print(total)
Why is the output 10?

The range contains no values, so the loop body never runs. The initialized total stays 10. Changing the range to range(4, 5) visits 4 once, giving 14. A trace with no iteration rows is a valid result, not a missing calculation.

Separate initialization, update and output

  • Initialization: total = 0 runs before the loop.
  • Update: add the current number only when the condition is true.
  • Output: the final print sits outside the loop, so it runs once.

If you move total = 0 inside the loop, each iteration discards the previous work. If you indent print(total) inside the loop, the program shows intermediate totals. Neither change is a syntax error; both change the meaning.

Practice A: change the condition

Keep range(1, 6), but use number % 2 != 0. Predict the final total and list which iterations change it.

Show the trace result

The condition now selects odd numbers. Iterations 1, 3 and 5 change the total: 0 → 1 → 4 → 9. The program prints 9.

Practice B: follow a changing condition

Trace this loop. Record the value before testing the condition and after both updates.

value = 3
steps = 0
while value < 20:
    value = value * 2
    steps = steps + 1
print(value, steps)
Show the working

The values after each iteration are 6, 12 and 24. The step counts are 1, 2 and 3. The next condition, 24 < 20, is false, so the output is 24 3. The condition is checked before the body; it does not stop an update from crossing 20.

Use the trace to explain a fix

When your output differs from the expected value, identify the first row where the state becomes wrong. Check that row's condition and assignment before changing several lines at once. Then rerun the smallest failing case and a boundary case. Keep the expected outputs beside your test inputs so “it runs” does not become your only check.

Language reference: Python's official tutorial on control flow and range. These trace examples are written for this guide and do not execute code in your browser.