Skip to content
dreamcode
dreamcode
Map
Break & Continue
Lesson 17 of 77
+15 XP on finish
PYTHON BASICSChapter 3 · Loops and iteration

Escaping loops

Use break to exit a loop immediately. Use continue to skip the rest of the current turn and jump straight to the next one.

Worked example

How it reads

  • continue skips printing 2 and jumps to the next turn
  • break exits the loop before printing 4
Cloud tip: break and continue work in both for loops and while loops.
main.py
PYTHON
real Python, runs in your browser
Console
Run your code to see its output here.
YOUR TURN

Loop over [4, 7, 9, 12, 15] and print the first number divisible by 3, then stop with break. It should print 9 and nothing else.

Press Run to check your work.