2. Making Decisions & Looping
while Loops, break, continue & Infinite Loops
Learn when to use a while loop instead of a for loop, trace how while conditions stop, avoid accidental infinite loops, and control loops cleanly with break and continue.
- Choose when a while loop fits better than a for loop
- Avoid accidental infinite loops by updating loop conditions
- Control execution inside loops using break and continue
A for loop is great when you know how many turns you need ("loop over these 5 prices").
But what if you don't know ahead of time how many turns it will take?
- "Keep asking for a password until the user types the right one."
- "Keep saving
$12a day until you reach$50."
For situations like that, Python has the while loop.
How a while Loop Thinks
A while loop checks its condition before every turn and exits as soon as it becomes False.
Read while condition: like this: "Keep repeating the indented lines as long as this condition is True."
count = 3
while count > 0:
print(f"Countdown: {count}")
count = count - 1
print("Go!")Let's walk through it slowly:
- Check
3 > 0→True→ printsCountdown: 3, then changescountto2. - Check
2 > 0→True→ printsCountdown: 2, then changescountto1. - Check
1 > 0→True→ printsCountdown: 1, then changescountto0. - Check
0 > 0→False! Python exits the loop and printsGo!.
The Infinite Loop Trap
Look at what happens if we forget count = count - 1:
count = 3
while count > 0:
print(f"Countdown: {count}")count stays 3 forever, so 3 > 0 is ALWAYS True—the loop will never stop! (If this ever happens in your terminal, press Ctrl + C to force-stop Python.)
Make sure something inside the loop changes your condition (like count = count - 1, saved = saved + 10, or asking fresh input()), so the loop eventually stops!
Stopping Early (break) & Skipping a Turn (continue)
Sometimes you need finer control inside a loop:
break: "Stop the whole loop right now and jump out."continue: "Skip the rest of THIS turn only, and jump back to the top for the next turn."
numbers = [5, 10, -2, 15, 20]
for n in numbers:
if n < 0:
print("Skipping negative number")
continue # Jumps back to the next number (-2 is not added/printed below)
if n == 15:
print("Found 15! Stopping early.")
break # Exits the loop completely (20 is never reached)
print(f"Number: {n}")Output:
Number: 5
Number: 10
Skipping negative number
Found 15! Stopping early.for vs while — Quick Cheat Guide
- Use
forwhen you have a list of items, a string, or a known count (range(10)). - Use
whilewhen you want to keep going until some condition changes (like reaching a savings goal or waiting for"quit").
Try It Yourself in the Playground
Watch how many days it takes to save $50 when saving $12 per day:
Click “Run Code” to execute this snippet right in your browser.
Mini-Challenge: Start with num = 1. Write a while loop that keeps multiplying num = num * 2 and uses break to stop and print the first number that goes above 100 (128).
Quick Checkpoints
What happens if the condition in a while loop never becomes False (and there is no break)?
Which keyword skips only the current turn of a loop and moves to the next turn?