Python Loop Control: break and continue
Sometimes you need fine-grained control over loop execution midway through a cycle. break immediately terminates the entire loop, jumping past its closing brace. continue skips the rest of the current iteration and jumps directly to the next cycle.
"Imagine searching through a deck of cards for the Ace of Spades. As soon as you find it, you shout "Found it!" and stop searching (break). If you draw a red card you don't care about, you toss it aside and immediately flip the next card (continue)."
Deep Dive: How It Works
break: Instant loop termination. Essential for linear search algorithms once the target item is found.
continue: Skips only the current iteration. The loop does NOT stop; it advances straight to the increment step and checks the condition for the next cycle.
Nested Loops: break only exits the innermost loop containing it.
Syntax Blueprint
for (int i = 0; i < 10; i++) {
if (i == 3) continue; // Skips 3
if (i == 7) break; // Stops loop at 7
std::cout << i << " ";
}continue skips the rest of the body; break stops the loop completely.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Using continue in a while loop without incrementing the counterWhy it happens: If continue skips past the line with i++, i never changes, causing an infinite loop!
How to fix: Ensure the counter increments before calling continue in while loops.
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.
Stop a loop at 3
Write a for loop from int i = 1 to 5.
If i == 3, print "Found 3" and break the loop.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.