I1 · Python Programming for AI

Tracing an Algorithm by Hand

Tracing, also called a dry run, means following an algorithm step by step by hand and recording the value of each variable after every step, usually in a trace table. It predicts the output and reveals errors before the program is run.

8 min Intermediate · Sublevel Beginner Jump to the activity

What you need: The earlier topics of this module.

Definition

Can you know what a program will output before the computer runs it?

Tracing, also called a dry run, means following an algorithm step by step by hand and recording the value of each variable after every step, usually in a trace table. It predicts the output and reveals errors before the program is run.

In simple words

Become the computer. Make a column for each variable. Go through the steps one at a time, and every time a value changes, write the new value on a new row. When you reach the end, the last row tells you the answer.

Overview

  • Tracing means running an algorithm yourself, step by step, writing down every variable's value as it changes.
  • It is also called a dry run because no computer is used.
  • A trace table has one column per variable and one row per step or loop pass.
  • Tracing reveals logic errors such as wrong loop limits, swapped conditions and variables that never change.
  • Good test inputs include normal values, boundary values and unusual or empty inputs.
  • The same skill later becomes using a debugger, which traces the program for you.

Why it matters

Tracing is how programmers understand code they didn't write and find bugs they can't see. It builds the exact mental model of a computer that every later topic, from loops to neural networks, relies on.

Key terms

Trace table
A table recording each variable's value after every step of an algorithm.
Dry run
Executing an algorithm by hand, without a computer.
State
The values of all variables at a particular moment.
Off-by-one error
A mistake where a loop runs one time too many or too few.
Logic error
A mistake that lets the program run but gives the wrong answer.
Test data
Inputs chosen to check an algorithm, including normal and edge cases.

Types

There are several ways to trace and check an algorithm, from pencil and paper to tools. Each catches different mistakes.

1. Trace table (dry run)

A table that records the value of each variable after every step.

  • How it works: Write the variable names as columns, then follow the algorithm line by line, adding a row whenever a value changes and noting outputs.
  • Example: Tracing a loop that sums 1 to 4: total goes 0, 1, 3, 6, 10 while i goes 1, 2, 3, 4.
  • When to use it: Use it for loops and small algorithms, and whenever an output looks wrong and you cannot see why.

2. Desk check with test cases

Choosing inputs whose correct answers you already know, then tracing to see whether the algorithm agrees.

  • How it works: List each test input with its expected output, trace the algorithm for each, and compare the result with the expectation.
  • Example: For a grading algorithm, test marks 95 (expect A), 50 (expect C) and 20 (expect F).
  • When to use it: Use it before writing code and again after any change, to confirm that the logic still gives the right answers.

3. Boundary (edge-case) tracing

Tracing with values at the exact limits, and with empty or extreme inputs.

  • How it works: Pick values on, just below and just above every limit in a condition, plus an empty list, zero and a very large number.
  • Example: For "pass if marks ≥ 33", trace 32, 33 and 34, and an empty list of marks.
  • When to use it: Use it for every condition and loop limit, because off-by-one mistakes hide exactly at boundaries.

4. Print tracing

Adding temporary print statements to a running program to show values at chosen points.

  • How it works: Insert prints that show variable names and values inside loops or before decisions, run the program, read the output, then remove the prints.
  • Example: print("i =", i, "total =", total) inside a loop shows each pass.
  • When to use it: Use it for quick checks in small programs once the code exists.

5. Debugger stepping

Using a tool that pauses the program and lets you run it one line at a time while watching variables.

  • How it works: Set a breakpoint on a line, start debugging, then step over or into each line while a panel shows every variable's current value.
  • Example: In VS Code, a breakpoint inside a loop shows how a list changes on every pass.
  • When to use it: Use it for larger programs and bugs that print tracing cannot pin down.

Explained in detail

Why trace by hand

A computer runs an algorithm far too fast to watch. Tracing slows it down to human speed. You become the computer: you read each step, carry it out exactly as written, not as you meant it, and write down every variable's new value. Because you follow the instructions literally, you discover what they really do. Tracing is the best way to find logic errors, which are the hardest bugs because the program runs without complaint and simply produces a wrong answer.

How to build a trace table

  1. Make one column for each variable, plus columns for any condition and for output.
  2. Write the starting values in the first row.
  3. Add a new row each time a variable changes or a condition is checked.
  4. Record outputs exactly as they would appear.
  5. Compare the final output with what you expected.

Example: counting passes

Suppose an algorithm should count how many marks in the list 45, 30, 72 are 40 or above. It sets count to 0, then for each mark, if mark > 40, adds 1 to count. Tracing: after 45, count is 1; after 30, still 1; after 72, count is 2. The output is 2, which is right for this list. But trace again with 40, 30, 72: the answer should be 2, yet the algorithm gives 1, because 40 > 40 is false. The bug is using > instead of >=. Only an edge case exactly on the boundary revealed it, which is why good test data always includes boundary values.

Choosing test data

KindPurposeExample for "pass if 40 or more"
NormalTypical values65, 22
BoundaryValues on and next to the limit39, 40, 41
ExtremeSmallest and largest allowed0, 100
Empty or unusualCases the author may not have imaginedAn empty list

Tracing remains useful long after you can program. Experienced developers still trace small pieces of tricky code on paper, and the debuggers in professional tools are, in effect, automatic trace tables that let you step through a program one line at a time and watch the variables change.

How it works

total ← 0
for each mark in [12, 7, 15, 5]:
    total ← total + mark
output total
Stepmarktotal
Start–0
1st loop1212
2nd loop719
3rd loop1534
4th loop539

Try it yourself

Trace a loop that totals a list of marks

Fill in the value of total after each step, then check your trace.

Trace a search for the largest mark

Now trace an algorithm with a decision inside the loop.

Common mistakes

  • Skipping a step in your head instead of writing every row.
  • Updating a variable with its old value from two steps ago.
  • Tracing with huge inputs. Small ones find the same bugs faster.

Check yourself

Question 1

1. total starts at 0 and adds 3, then 4, then 5. Its final value is…

Question 2

2. What does a trace table record?

Question 3

3. largest starts at 12, then the marks 7, 15, 5 are checked. Final largest?

Summary

  • Tracing means running an algorithm by hand, step by step.
  • A trace table has a column per variable and a row per change.
  • It predicts output and pinpoints bugs before running anything.

Finished reading?

Sources: Python tutorial, loops and control flow