Execution Trace
An execution trace is a record of a program run, statement by statement: which line the program counter is on, and what every variable holds after each step. Master that two-part picture — counter plus state — and any program, however large, becomes something you can follow mechanically, one statement at a time.
The program counter is your finger on a recipe. It rests on one instruction, you carry that instruction out, and the finger moves to the next line. The only drama is "repeat until done" — the instruction that sends your finger back up the page. This lesson makes the finger visible: an arrow marks the executing line, a State panel shows each variable, and a console collects the output.
How an Execution Trace Works
By default the counter just moves to the next line down. Control flow is what breaks that monotony. A while loop checks its condition: if it holds, the counter steps into the loop body; when the body finishes, the counter jumps back up to re-check; when the condition finally fails, the counter jumps past the body to whatever comes next. That up-and-down travel of the finger is the loop — not a special construct in the machine, just a counter that sometimes moves backwards.
The traced program sums the numbers from n down to 1. It sets a running total to zero, then, while n > 0, adds n to total and decrements n. Here n is the input, set by the slider — the five lines of code never change; only the values flowing through them do.
Execution Trace Step by Step
With the default input n = 3:
- Line 1 —
total = 0createstotaland sets it to 0. - Line 2 —
while n > 0:3 > 0is true, so the counter steps into the body. - Line 3 —
total = total + n: the right-hand side evaluates with the current values,0 + 3, sototalbecomes 3. - Line 4 —
n = n - 1:nbecomes 2. The body is done; the counter jumps back up to line 2. - Second pass —
2 > 0is true;total = 3 + 2 = 5;nbecomes 1. - Third pass —
1 > 0is true;total = 5 + 1 = 6;nbecomes 0. - Line 2 —
0 > 0is false. The counter skips the body entirely. - Line 5 —
print(total)writes 6 to the console. Done.
The arrow tells the story by itself: it walks down through lines 1–4, jumps back up to line 2 three times, then drops past the body to line 5 when the condition fails. Each jump beat runs half again as long as an ordinary one, so the long travel reads as a jump rather than a flicker. Meanwhile the State panel flashes whichever variable just changed, and the trace ledger records every evaluation — 0 + 3 → 3, 3 > 0 → true — using the values as they stood before the statement ran.
What the Interpreter Actually Does
Count the executed statements and the trace has a shape: one assignment, n + 1 condition checks (every pass plus the final failing one), 2n body statements, and one print — 3n + 3 statements for input n. The program text is five lines forever; the trace is what grows.
| Input | Body passes | Statements executed | Output |
|---|---|---|---|
n = 1 | 1 | 6 | 1 |
n = 3 | 3 | 12 | 6 |
n = 6 | 6 | 21 | 21 |
That distinction — static text, dynamic trace — is the core mental model. Real interpreters add machinery underneath (bytecode compilation, a call stack of frames, aggressive optimization), but the contract they honor is exactly this one: a counter walking the code while state changes one statement at a time. Every later topic, from function calls to recursion, is an elaboration of this picture.
Edge Cases
n = 0—0 > 0is false on the very first check, so the body never runs and the program prints 0. The counter jumps straight from line 2 to line 5: the cleanest proof that the condition is checked before the body, not after.n = 1— exactly one pass: add 1, decrement to 0, exit on the next check.- The slider caps at
n = 6— beyond that, nothing new happens; the same five lines just loop more times andtotalgrows.
Common Mistakes
- Forgetting
n = n - 1. Without the decrement, a positivennever reaches 0, the condition stays true forever, and the loop never terminates. That one line is what makes the recipe finish. - Reading a
whilelike anif. Anifruns its block at most once and moves on; awhilesends the finger back to its condition after every pass. The loop-back is the entire difference. - Evaluating the right-hand side with the new value.
total = total + nreads the oldtotalfirst, then writes —0 + 3, not3 + 3. Assignment is read-then-write, never simultaneous. - Confusing the program counter with a variable. The counter is the machine's bookmark for "which line runs next" — it lives outside your program's data, which is why it never appears in the State panel.
A Note on Simplification
The machine on screen is the contract, not the implementation. A real Python interpreter compiles the source to bytecode, runs a stack of frames, and optimizes as it goes — it does not literally slide a pointer down your file. What it must preserve is exactly what the animation shows: statements taking effect one at a time, in the order the arrow visits them, with state changing at each step. That contract is the level at which you read and debug code, so it is the level this lesson draws.
Keep exploring
A few lessons that share the same technique or lead into the next idea.
Found a mistake or something unclear? Leave feedback.