1.2.17 · D2Introduction to Programming (Python)

Visual walkthrough — while loop — condition-based, infinite loop dangers

2,061 words9 min readBack to topic

This page builds on the parent topic. Everything here is drawn from zero — you do not need to remember any code.


Step 1 — The three ingredients, laid out in space

WHAT: We set up our running example — count and print 1, 2, 3.

i = 1            # Initialise: box i holds 1
while i <= 3:    # Condition: is i still ≤ 3 ?
    print(i)     # Body
    i = i + 1    # Update: box i grows by 1

WHY these three: A condition alone is a stop sign with no car driving toward it. The update is the engine; the initialise is where the car starts. Remove any one and the machine breaks — we will watch that break in Step 6.

PICTURE: Below, the box i starts at . The green arrow is the condition-question pointing at the box; the orange arrow is the update pushing the value upward.

Figure — while loop — condition-based, infinite loop dangers

Step 2 — The loop as a circular track

WHAT: We redraw the four lines of code as a cycle of four stations.

WHY a circle and not a list: A list suggests "do this, then this, then done." But the update sends control back to the condition — that back-jump is invisible in the flat text. Drawing the circle makes the repeat literal, so you can count laps.

PICTURE: Follow the arrows. Start at CHECK (the diamond). If the answer is yes (True), go into the loop body, print, update, and the orange arrow loops you back to CHECK. If the answer is no (False), the teal arrow leaves the circle forever.

Figure — while loop — condition-based, infinite loop dangers

Step 3 — Lap 1: the first check passes

WHAT: i holds . The question is i <= 3.

The symbol <= means "less than or equal to". So i <= 3 reads as:

WHY it matters: True is the ticket into the body. Because the answer is True, we print(1) and then the update makes the box become .

PICTURE: The box glows green (the check passed). Below the box, a small trace line records: printed 1, i is now 2.

Figure — while loop — condition-based, infinite loop dangers

Step 4 — Laps 2 and 3: the box climbs toward the wall

WHAT: Two more laps.

Lap box i before check i <= 3 ? printed box i after update
2 = True 2
3 = True 3

Notice lap 3: is True because <= includes equal. This is the moment people get wrong — they think "3 is the limit so it stops here." No: the value is still allowed to equal the limit.

WHY show the equal case: The boundary value is the trickiest one. Getting it right or wrong is the difference between printing 1 2 3 and printing only 1 2. This is exactly an off-by-one error.

PICTURE: The box rises like a bar chart across the laps, with a dashed red wall at the limit . Green bars are "still allowed"; the bar for value sits on the wall and is still green.

Figure — while loop — condition-based, infinite loop dangers

Step 5 — The final check: the one that fails and ends it

WHAT: The box now holds . We check one last time:

WHY: False means "no ticket" — control takes the exit arrow and the loop is over. The body does not run this lap, so 4 is never printed.

PICTURE: The box for value has climbed past the wall. It glows red (check failed). The teal exit arrow carries us out of the circle.

Figure — while loop — condition-based, infinite loop dangers

Step 6 — The degenerate case: forget the update → infinite loop

WHAT: We remove the line i = i + 1.

i = 1
while i <= 3:
    print(i)     # no update!

Every lap the box still holds , so every check is , forever.

WHY it never stops: The car has no engine. It never moves toward the wall, so the condition never turns False. This is the infinite loop. In the terminal you escape it with Ctrl + C.

PICTURE: The circular track from Step 2, but the update station is crossed out and a red arrow loops back with the box stuck at — an endless spin, the value flat.

Figure — while loop — condition-based, infinite loop dangers

Step 7 — The zero-lap case: first check already False

WHAT: Start the box at instead of .

i = 9
while i <= 3:    # 9 <= 3 is False on the very first check
    print(i)     # never runs

WHY show this: It proves the condition truly comes before the body. A for loop over an empty list behaves the same way — see for loop — iterating over sequences. Never assume "a loop runs at least once"; a while may run zero times.

PICTURE: The circular track entered at CHECK, the diamond immediately says False, and the teal exit arrow fires straight out — the body station never lights up.

Figure — while loop — condition-based, infinite loop dangers

The one-picture summary

Everything above, compressed into a single timeline: the box climbs (all green, all printed), reaches (red, the failing check), and the exit fires. Below the climb sits the check-counter showing 4 checks → 3 prints, and to the side the two failure modes: frozen box (no update = spin forever) and starting past the wall (zero laps).

Figure — while loop — condition-based, infinite loop dangers
Recall Feynman retelling — say it to a 12-year-old

You're climbing stairs, and painted on the wall is a rule: "Keep going while your step number is 3 or less." Before every step you glance at the rule. Step 1 — allowed, climb, now you're on step 2. Glance again — allowed, climb, step 3. Glance — 3 is still allowed (it says or less and equal), climb, step 4. Glance — 4 is NOT allowed, so you stop and walk off. You glanced four times but only climbed three — that extra glance is the one that stopped you. Now two ways to break it: (1) if you never actually climb — you just keep glancing at step 1 forever — you're stuck spinning: that's the infinite loop, and you smash Ctrl + C to escape. (2) If you start on step 9, your very first glance says "not allowed," and you never climb at all — zero steps. That's the whole loop: glance, climb, glance, climb… until a glance says no.


Connections