1. Foundations
total = 0
while n > 0:
    total = total + n
    n = n - 1
print(total)
Code
Inputs
Log

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:

  1. Line 1 — total = 0 creates total and sets it to 0.
  2. Line 2 — while n > 0: 3 > 0 is true, so step into the body.
  3. Line 3 — total = total + n: 0 + 3, total becomes 3.
  4. Line 4 — n = n - 1: n becomes 2. The counter jumps back up to line 2.
  5. Line 2 — 2 > 0 is true → Line 3 total = 3 + 2 = 5 → Line 4 n becomes 1.
  6. Line 2 — 1 > 0 is true → Line 3 total = 5 + 1 = 6 → Line 4 n becomes 0.
  7. Line 2 — 0 > 0 is false: exit the loop. The counter skips the body.
  8. Line 5 — print(total) outputs 6.

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.

InputIterationsOutput
n = 111
n = 336
n = 6621

Edge Cases

  • n = 0 — 0 > 0 is 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.
  • 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 of total) grows.

Common Mistakes

  • Forgetting n = n - 1 — without the decrement, n never reaches 0, the condition stays true, and the loop runs forever. That line is what makes the loop terminate.
  • Reading a while like an if — an if runs its block at most once; a while returns 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.