Python Loop Control: break and continue
Fine-grained loop control gives you power over execution flow. break terminates the loop immediately and jumps past its closing brace. continue skips the rest of the current cycle and jumps directly to the next iteration.
"Searching through a box for your lost keys: As soon as your hand grabs the keys, you shout "Found them!" and stop searching (break). If you pick up a piece of junk mail, you drop it and instantly grab the next item (continue)."
Deep Dive: How It Works
break: Halts the loop immediately. Crucial for search algorithms once the target is found to avoid wasting CPU cycles.
continue: Skips the remaining code inside the loop body for this cycle only; loop advances to the next iteration.
Nested Loops: break only breaks out of the innermost loop containing it.
Syntax Blueprint
for (int i = 0; i < 10; i++) {
if (i % 2 != 0) continue; // Skip odds
if (i == 6) break; // Stop at 6
Console.WriteLine(i);
}continue skips to next cycle; break terminates the entire loop.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Using continue in a while loop after the counter incrementWhy it happens: If continue skips before i++, i never increments, causing an infinite loop.
How to fix: Ensure loop counter increments before 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 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.