Infinite Loop —
No exception. No crash. Just... never stops.
Your while loop's condition never becomes false, so it just keeps running forever — no traceback, no error message, just a program that hangs. It's the one bug on this hub that doesn't raise an exception at all. Watch a frozen loop variable on LearnBug and see exactly which line should have changed it, but didn't.
What is an Infinite Loop?
A while loop keeps running as long as its condition evaluates to True. If nothing inside the loop body changes any variable the condition depends on, that condition stays true forever, and the loop never exits — the program appears to "freeze," consuming CPU with no output and no error.
Why visualization helps
There's no traceback to read here — the only way to understand it is to watch the loop's state over multiple iterations and notice what isn't changing. LearnBug lets you step through iterations and see variable values stay frozen, instead of the program just appearing to hang with no information.
Three ways the condition never changes
Forgetting to update the loop variable
A counter that should increment each iteration (like count += 1) is simply missing from the loop body.
Updating the wrong variable
The loop checks while count < 5 but the body updates a differently-named or misspelled variable instead of count.
A condition that can never become false
while True without any break statement inside, or a condition with faulty logic that's always true regardless of the variable's value.
while count < 5 — count never changes
count = 0. Check: is 0 < 5? Yes — enter the loop, print 0
This is a completely normal first iteration. Nothing looks wrong yet — the condition check and the print both behave exactly as expected.
The loop body prints count, but never increments it
This is the actual bug — the line count += 1 is simply missing. There's no error here, because printing a value is a completely valid operation; it's just not doing what the programmer intended.
Check again: is 0 < 5? Still yes — iteration 2 begins, identical to iteration 1
Since nothing changed count, the condition evaluates exactly the same way it did before. This will repeat identically, forever.
The program never reaches any code after the loop — it just runs forever
There's no traceback to read because nothing ever raises an exception — the program is simply stuck, consuming CPU indefinitely until manually interrupted.
Result: No error, no output change — the program hangs ✗
The broken code — no traceback, it just hangs
count = 0
while count < 5:
print(count)
# forgot count += 1 — condition never becomes False
# This never prints anything past here. No traceback — it just runs forever.Make sure the loop body actually changes the condition
count = 0
while count < 5:
print(count)
count += 1 # now the condition eventually becomes False
print("Done!")Quick Reference
Frequently asked questions
How do I even notice an infinite loop is happening?
The program stops producing new output, or a script that should finish quickly just keeps running with no end in sight and no increase in CPU-visible progress. In a terminal, Ctrl+C interrupts a hung Python program and shows a KeyboardInterrupt traceback pointing at whichever line was executing — usually right inside the loop.
Is while True: always a sign of a bug?
No — while True combined with an explicit break statement somewhere inside is a completely valid, common pattern (useful when the exit condition is easier to check partway through the loop body than at the top). It only becomes a bug if the break condition can never actually be reached.
Can a for loop have this same problem?
Not in the same way — a for loop iterates over a fixed, already-known sequence (a list, a range, etc.), so it's guaranteed to terminate once that sequence is exhausted. Infinite loops are specifically a while-loop problem, because while's exit condition depends on program state that must be correctly maintained by the programmer.
How can I add a safety net to prevent truly infinite loops during development?
Add a maximum-iteration counter as a temporary safeguard — if iterations > 10000: break — while developing and testing, so a logic bug produces a clear stopping point instead of an indefinite hang. Remove it once you're confident the real exit condition is correct.
Watch your loop's variables — and see what stops changing
Paste your loop into LearnBug and step through iterations to spot the frozen value.