Python Loop Control: break, continue & Labeled Jumps
`break` immediately terminates the innermost enclosing loop, while `continue` skips the remainder of the current iteration and proceeds to the next cycle. When working with deeply nested loops, Labeled Breaks (`break outerLoop;`) enable breaking out of multiple loop layers cleanly without artificial boolean flags.
"`continue` is like skipping a track on a playlist to hear the next song; `break` is turning off the stereo entirely."
Deep Dive: How It Works
`break`: Exits the loop structure entirely.
`continue`: Bypasses remaining code in the loop body and advances the iterator/counter.
Loop Labels: Declared with an identifier followed by a colon (`outer: for (...)`), enabling targeted `break outer;` or `continue outer;`.
Syntax Blueprint
outerLoop: for (var r = 0; r < 5; r++) {
for (var c = 0; c < 5; c++) {
if (r == 2 && c == 2) break outerLoop; // Breaks BOTH loops
}
}Labeled loops allow breaking out of multiple nested loops simultaneously.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Using unlabeled `break` in a nested loop and expecting the outer loop to terminate.Why it happens: Unlabeled `break` only exits the innermost loop.
How to fix: Apply a label to the outer loop (`outer: for (...)`) and execute `break outer;`.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Sum First Three Positive Numbers
Given list `final numbers = [-2, 5, -1, 10, 15, 20, 25];`.
Iterate through the list. If `n <= 0`, use `continue`.
Add `n` to `sum`, and `count++`. When `count == 3`, use `break`.
Print `Sum: $sum`.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.