Repeating with while Loops

Python 3.14 · ✓ verified by execution on 2026-07-22

An if runs its block once when a condition is true. A while loop runs its block over and over, as long as the condition stays true — rechecking before every pass. It’s how you repeat work without copying and pasting lines.

python
count = 0
while count < 3:
    print(count)
    count += 1
Output
0
1
2

Exiting Early with break

Sometimes you want to stop looping before the condition turns false. A break statement immediately exits the loop, skipping anything left in the current pass.

python
n = 5
while n > 0:
    if n == 3:
        break
    print(n)
    n -= 1
Output
5
4

Skipping Iterations with continue

python
i = 0
while i < 3:
    i += 1
    if i == 2:
        continue
    print(i)
Output
1
3

The while-else Construct

A while loop can have an else block. It runs once, when the loop finishes because its condition became false — but not if the loop was cut short by a break. That makes else a handy “we finished without stopping early” signal.

python
x = 0
while x < 2:
    print(x)
    x += 1
else:
    print('Done')
Output
0
1
Done

Common Misconceptions

Myth: A while loop stops on its own even if you never change the condition. Reality: If the condition never becomes false and nothing breaks, you get an infinite loop. Always make sure something inside the loop moves it toward finishing.

Myth: The else block runs when the loop ends by a break. Reality: It’s the opposite — else runs only when the loop ends normally. A break skips the else.

Edge Cases

Under the Hood

Watch how the program counter jumps back to the condition check at the end of each iteration, and how a break statement jumps out of the loop entirely:

python · visualize
n = 3
while n > 0:
    if n == 1:
        break
    print(n)
    n -= 1
print("Done")

Check yourself

What happens if a while loop's condition never becomes False and there is no break statement?

Reveal answer

It creates an infinite loop that runs forever until interrupted. — Unlike some bounded loops, a while loop will continuously execute as long as its condition evaluates to True. This results in an infinite loop if unhandled.

What is the purpose of the 'break' statement in a while loop?

Reveal answer

To exit the loop immediately, regardless of the condition. — The break statement terminates the loop completely, jumping execution to the first statement after the loop body.

When does the 'else' block of a while loop execute?

Reveal answer

When the loop's condition becomes False. — The else block executes only when the while condition is evaluated to False. If the loop is terminated by a break statement, the else block is skipped.

Challenges

Challenge 1 +50 XP

Use a while loop to print the numbers 10, 9, 8, 7, in that order.

python
  • Test 1 — expects "10\n9\n8\n7"
Need a hint? (−25% XP)

Use a while loop that checks if n is greater than 6, and remember to decrement n inside the loop.

Show solution (0 XP)
n = 10
while n > 6:
    print(n)
    n -= 1

Challenge 2 +100 XP

Given `i = 1`, use a while loop to print odd numbers less than 6.

python
  • Test 1 — expects "1\n3\n5"
Need a hint? (−25% XP)

Increment i by 2 in each iteration.

Show solution (0 XP)
i = 1
while i < 6:
    print(i)
    i += 2