CurriculumPython while Loops, break and continue for Kidsbreak — leaving early

Python break Statement: Leaving a Loop Early

Stopping a loop the instant something happens, before its condition would end it naturally.

Lesson 2 of 5PROAges 10+
See what PRO unlocks →

break — leaving early

Sometimes you want to stop a loop RIGHT NOW, before its condition would naturally end it. That's break.

break exits the loop immediately — right where it is, mid-pass, no matter what the condition says.

room = 0
while room < 5:
    print(room)
    if room == 3:
        break
    room = room + 1

Output

0
1
2
3

room never reaches 5 here — the loop stopped itself the moment room hit 3, three steps before the condition would have.

room = 0
while room < 5:
    print(room)
    if room == 3:
        break
    room = room + 1

Not every while loop needs a break, though. Plenty just run their natural course and stop on their own condition.

Check your understanding

room = 0, while room < 5. The loop prints 0, 1, 2, 3 and then stops. Did room ever reach 5?

room = 0
while room < 5:
    print(room)
    if room == 3:
        break
    room = room + 1
  • No — break exited the loop while room was still 3
  • Yes — the loop always runs until its condition becomes False, break or not
  • It's impossible to tell without running the code

Why: break stops the loop the instant it runs, regardless of the loop's own condition. room was still 3 — well short of 5 — when the loop ended. That's the whole point of break: an early, deliberate exit.

What you'll practice

The dragon searches 5 rooms, numbered 0 to 4, printing each room number — but STOPS the instant it reaches room 3, where the treasure is. Print rooms 0, 1, 2, 3, then stop — never reach 4.

Unlock this lesson with PRO →

More from this topic

break — leaving early is one lesson inside Python while Loops, break and continue for Kids — see the full lesson order and what the whole topic covers.

View the full topic →