Visual walkthrough — while loop — condition-based, infinite loop dangers
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 1WHY 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.

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.

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.

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.

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.

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.

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.

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).

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
- Parent topic (Hinglish)
- for loop — iterating over sequences — the known-count cousin that also can run zero times
- break and continue statements — another way to leave the circle early
- Boolean expressions and comparison operators — what the check is built from
- input() and type conversion — a common real update inside the body
- Off-by-one errors — the "+1 check" made this whole page