Execution Trace
Intuition
A program is just a list of statements that a computer runs one at a time, top to bottom. To follow it, you only need to track two things: which statement is running right now — the program counter — and the current value of every variable — the state. Run the statement, update the state, move the counter to the next statement, repeat. This lesson makes both visible: an arrow marks the line executing right now, and a panel shows each variable as it changes.
How It Works
The program counter usually just moves to the next line down. The interesting part is control flow — statements that make the counter jump somewhere else. 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 the condition. When the condition is finally false, the counter jumps past the body to the next statement. That up-and-down travel of the arrow is the loop.
Our program sums the numbers from n down to 1. It keeps a running total, adds n to it, decreases n by one, and repeats while n > 0. Here n is the input — you set it with the slider — so the program text never changes; only the values flowing through it do.
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 step into the body. - Line 3 —
total = total + n:0 + 3,totalbecomes 3. - Line 4 —
n = n - 1:nbecomes 2. The counter jumps back up to line 2. - Line 2 —
2 > 0is true → Line 3total = 3 + 2 = 5→ Line 4nbecomes 1. - Line 2 —
1 > 0is true → Line 3total = 5 + 1 = 6→ Line 4nbecomes 0. - Line 2 —
0 > 0is false: exit the loop. The counter skips the body. - Line 5 —
print(total)outputs6.
Watch the arrow: it steps down through lines 1–4, jumps back up to line 2 three times, then skips down to line 5 once the condition fails.
Complexity
The body runs once per value of n, so an input n takes about n iterations — roughly 3·n + 3 beats overall. A larger n means a longer trace and a bigger total; the five lines never change.
| Input | Iterations | Output |
|---|---|---|
n = 1 | 1 | 1 |
n = 3 | 3 | 6 |
n = 6 | 6 | 21 |
Edge Cases
n = 0—0 > 0is false on the very first check, so the body never runs and the program prints0. The counter jumps straight from line 2 to line 5.n = 1— exactly one pass: add 1, decrement to 0, then exit on the next check.- Large
n— the same five lines execute; only the number of loop-backs (and the size oftotal) grows.
Common Mistakes
- Forgetting
n = n - 1— without the decrement,nnever reaches 0, the condition stays true, and the loop runs forever. That line is what makes the loop terminate. - Reading a
whilelike anif— anifruns its block at most once; awhilereturns to its condition after every pass. The loop-back is the whole difference. - Expecting the body to run when the condition is false — the check happens before the body, every time, so a false condition skips the body entirely.
- Confusing the program counter with a variable — the counter is the computer's bookmark for "which line is next," not part of your program's data.
A Note on Simplification
This is the statement-by-statement model of execution. Real interpreters do more under the hood — they compile your source to bytecode, manage a call stack of function frames, and optimize aggressively. But the mental model here — a counter walking the code while state changes one statement at a time — is exactly how the language behaves, and it's the foundation every later topic builds on. It's an explainer, not a substitute for a course on language internals.
These lessons are still being refined and may contain mistakes.