Question bank — while loop — condition-based, infinite loop dangers
Prerequisites worth having open: Boolean expressions and comparison operators (what a condition is), break and continue statements (how loops exit early), and Off-by-one errors (why the last check matters). Compare intent against the for loop.
True or false — justify
Every while loop body runs at least once.
while False: runs the body zero times. This is the opposite of a do-while loop found in some other languages.A while loop always ends.
False. while True: with no break runs forever — that's an infinite loop by design or by accident.The condition of while i <= 3: (starting i=1, i grows by 1) is evaluated exactly 3 times.
True (printing 1, 2, 3) and once it's False (when i reaches 4), and that final failing check is what ends the loop.while True: is always a programming mistake.
break. It's only a bug when the loop was meant to stop but nobody wrote an exit.If a condition is True when the loop starts, changing the variable after the loop can stop it.
A while loop and an equivalent for loop over range will always run the body the same number of times.
for loop's count is fixed by range, while the while depends on your update line being correct — one wrong update and the counts diverge.Using and in a condition means both parts are always evaluated.
A and B skips B if A is already False. This matters when B would crash (e.g. dividing by zero) — order your conditions so the safe check comes first.Spot the error
i = 0
while i < 5:
print(i)::: Missing update. Nothing ever changes i, so i < 5 stays True forever — an infinite loop printing 0 endlessly. Add i += 1 inside the body.
while count < 10:
print(count)
count += 1::: count is never initialised. The very first check count < 10 reads a variable that doesn't exist yet → NameError. You must create count = 0 (the "Initialise" step) before the loop.
while x = 5:
x -= 1::: Assignment used as condition. = assigns, == compares. Python raises a SyntaxError — which is actually a lucky save, because it stops you before the loop can misbehave.
n = 3
while n > 0:
print(n)
if n == 2:
continue
n -= 1::: Update skipped by continue. When n == 2, continue jumps back to the check before reaching n -= 1, so n sticks at 2 forever. The update must sit on the path that always runs. See break and continue statements.
x = 0.0
while x != 1.0:
x += 0.1::: Float never lands exactly on 1.0. Adding 0.1 in binary floating-point accumulates tiny errors, so x skips past 1.0 (e.g. to 0.9999... then 1.0999...) and != stays True forever. Use while x < 1.0: instead.
password = ""
while password != "open"
password = input("Password: ")::: Missing colon. Every while header ends with :. Without it Python raises a SyntaxError and the loop won't even start.
i = 10
while i > 0:
print(i)
i += 1::: Update pushes the wrong direction. i starts at 10 and grows, so i > 0 only gets more true — infinite loop. To reach the stop condition you need i -= 1.
Why questions
Why is a while loop called a pre-test loop?
False.Why does forgetting the update line cause an infinite loop, even when the condition looks correct?
Why prefer while over for for reading user input until they type "quit"?
for needs a known number of iterations; while loops on a condition, which is exactly "unknown count, stop when something happens". See input() and type conversion.Why should you never compare floats with == in a loop condition?
< or > catches the boundary regardless of tiny rounding errors.Why does an accumulator like while total < 100: total += x guarantee it stops?
total strictly increases every iteration (monotonic approach to the boundary). A value that always moves toward the limit must eventually cross it, flipping the condition to False.Why is the final condition check (the one that fails) so important to count?
Why does break let us safely write while True:?
while True: can never end on its own, so break provides the real exit from inside the body — a controlled stop triggered by whatever event you choose, instead of by the condition. See break and continue statements.Edge cases
What does while False: do?
What happens with while 1: (a number, not True)?
1 counts as True, so it behaves exactly like while True:.What does while []: (an empty list) do?
False immediately and the body never runs. Empty containers, 0, None, and "" are all falsy. See Boolean expressions and comparison operators.In while n > 0: with n starting at exactly 0, how many times does the body run?
0 > 0 is False right away, so it's a valid, deliberate "run only if there's work to do" — not a bug, just the boundary case.If a break sits before the update line, does the update ever execute on that iteration?
break exits immediately, skipping every line below it — including the update. That's fine when you want to leave, but a trap if you expected cleanup code after break.Can a while loop with a correct update still run forever?
continue skips over it on some iterations. "Has an update line" is not the same as "reaches the stop condition".Recall Self-test — cover the answers
- Does
while False:run its body? ::: No — pre-test fails first, zero iterations. - Why can't a line after the loop stop a running loop? ::: It only executes once the loop already exited; only body code affects a live loop.
- Why is
!=dangerous with float updates? ::: Floats overshoot the exact target, so!=stays true forever; use</>. - Is
while 1:an infinite loop? ::: Yes — non-zero numbers are truthy, same aswhile True:.
Connections
- 1.2.17 while loop — condition-based, infinite loop dangers (Hinglish) — same topic in Hinglish
- for loop — iterating over sequences — the known-count counterpart these traps contrast against
- break and continue statements — behind the
continue-skips-update andbreak-before-update traps - Boolean expressions and comparison operators — truthy/falsy edge cases and short-circuit evaluation
- input() and type conversion — the driver behind unknown-count validation loops
- Off-by-one errors — why counting the final failing check matters