A Loop That Watches a Condition
Where for iterates over a known sequence, while keeps looping as long as a condition is true. You reach for it when you don't know in advance how many iterations you'll need — retrying until something works, reading input until the user says stop, polling a service until it responds.
The shape is simple:
That prints 0 through 4. Python checks the condition at the top of each iteration. If it's true, the body runs. Then it checks again. When the condition becomes false, the loop ends.
Notice that count += 1 matters. Without it, the condition would stay true forever — an infinite loop. That's the main trap with while, and the main reason to prefer for when you can use it.
When while Is the Right Tool
A few classic use cases:
Retry until success.
Read input until the user exits.
Process a queue.
The common thread: you're watching for a state change rather than iterating over items.
while True Is Fine
You'll see this pattern a lot:
This is idiomatic Python when the "real" exit condition is inside the loop body, not something clean you can test at the top. It's easier to read than inventing a should_continue flag you update in one place and check in another.
The only rule: make sure there is a break (or return, or a raised exception) that the code will eventually reach. An unconditional while True: with no way out is the classic way to lock up a program.
Avoiding the Infinite Loop
Infinite loops happen when the condition stays true forever. The usual suspects:
- Forgot to update the variable.
while count < 5:needs something inside the body that changescount. - Updated the wrong variable.
while i < 10:and thenj += 1inside — easy to miss if your code has more than a few variables. - Floating-point comparison.
while x != 1.0:can loop forever ifxonly ever gets close to 1.0. Use<=orabs(x - 1.0) < toleranceinstead.
If you end up stuck in an infinite loop in a terminal, press Ctrl+C. That sends an interrupt signal, and Python stops whatever it's doing.
break, continue, and else
while supports the same three "flow" tools as for:
breakexits the loop immediately.continueskips to the next iteration (so the condition is checked again).elseruns if the loop finished without abreak.
That's a primitive primality check. If the inner break fires, the number is composite. If the loop exits naturally (condition becomes false), the else runs and declares the number prime.
while vs for: Which to Pick?
A quick mental test:
- Do you have a collection to step through? →
for. - Do you need to iterate a fixed number of times? →
for i in range(n):. - Are you looping until some condition changes? →
while.
In early Python code you'll find that for covers 90% of loops. while shows up when you're doing something like polling, retrying, or building a simple REPL. If you're using while to iterate over a list with an index, that's a sign you should switch to for + enumerate.
A Practical while Program
A number-guessing game is a classic example because the number of iterations depends on how the user plays:
You can't know in advance how many tries it'll take — it depends on the user's guesses. That's while's whole reason for existing.
Looking Ahead
You've now seen the two loop types Python gives you. Next up: range() in detail — it comes up in nearly every counting-style for loop, and the small nuances are worth understanding before we move into collections.
Frequently Asked Questions
When should I use a while loop instead of a for loop?
Use while when you don't know in advance how many iterations you need — for example, retrying until something succeeds, or reading input until the user types 'quit'. Use for when you're stepping through a known collection.
How do I avoid an infinite loop in Python?
Make sure something inside the loop eventually makes the condition false. The most common cause of infinite loops is forgetting to update the variable the condition depends on. If you hit one, press Ctrl+C in the terminal to stop it.
Is while True bad practice?
while True bad practice?Not inherently. while True: with a clear break inside is a perfectly normal pattern for "loop until we hit a specific exit condition." It's cleaner than inventing a boolean flag you have to manage in two places.