The dragon explains
break — leaving early
The dragon: Sometimes you want to stop a loop RIGHT NOW, before its condition would naturally end it. That's break.
The dragon: break exits the loop immediately — right where it is, mid-pass, no matter what the condition says.
example.pyPython 3room = 0while room < 5: print(room) if room == 3: break room = room + 1
Output0 1 2 3The dragon: room never reaches 5 here — the loop stopped itself the moment room hit 3, three steps before the condition would have.
example.pyPython 3room = 0while room < 5: print(room) if room == 3: break room = room + 1
The dragon: Not every while loop needs a break, though. Plenty just run their natural course and stop on their own condition.
Quick check
Check your understanding
Say your answer out loud before reading the marked one. Every wrong option is a mistake real beginners make.
Question 1 of 1
room = 0, while room < 5. The loop prints 0, 1, 2, 3 and then stops. Did room ever reach 5?
question.pyPython 3room = 0while room < 5: print(room) if room == 3: break room = room + 1
- No — break exited the loop while room was still 3Correct
- 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.
In the game
What you'll practice
Three levels in the game: one guided first try with the shape of the answer already there, then two to solve on your own.
- 1
GuidedYour first try
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.
- +2
PracticeTwo more, on your own
This lesson is part of PRO.
PRO is a one-time $49.99 purchase, no subscription, and unlocks this whole topic along with everything else. Topics 1–3 are free, start to finish.
Unlock this lesson with PROStuck three times? The game offers the answer — unasked.