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.

3 min readUpdated 2026-11-29
In This Lesson
  • 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 $12 a day until you reach $50."

For situations like that, Python has the while loop.

How a while Loop Thinks

How a while Loop Repeats
Check Condition
True → Run Body
False → Exit Loop

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

python
count = 3

while count > 0:
    print(f"Countdown: {count}")
    count = count - 1

print("Go!")

Let's walk through it slowly:

  1. Check 3 > 0 → True → prints Countdown: 3, then changes count to 2.
  2. Check 2 > 0 → True → prints Countdown: 2, then changes count to 1.
  3. Check 1 > 0 → True → prints Countdown: 1, then changes count to 0.
  4. Check 0 > 0 → False! Python exits the loop and prints Go!.

The Infinite Loop Trap

Look at what happens if we forget count = count - 1:

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

Golden Rule for while Loops

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."
python
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:

text
Number: 5
Number: 10
Skipping negative number
Found 15! Stopping early.

for vs while — Quick Cheat Guide

  • Use for when you have a list of items, a string, or a known count (range(10)).
  • Use while when 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:

Python 3Try It Live: Daily Savings Goal
Output

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

Check Your Understanding

What happens if the condition in a while loop never becomes False (and there is no break)?

Check Your Understanding

Which keyword skips only the current turn of a loop and moves to the next turn?