break, continue, and Early Exit
Control loops precisely.
break, continue, and Early Exit is a free Mojo Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Mojo Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Steer a Loop Precisely
Sometimes you must leave a loop early or skip a single pass. Mojo gives you break and continue for exactly that control. 🎛️
Stop Early with break
The break statement exits the loop immediately. No further passes run, and execution jumps to the code after the loop.
for i in range(10):
if i == 3:
breakA Classic Search
break shines when searching: scan until you find the target, then stop so you do not waste passes on the rest.
for name in names:
if name == target:
breakSkip a Pass with continue
The continue statement skips the rest of the current pass and jumps straight to the next iteration of the loop.
for i in range(5):
if i == 2:
continue
print(i)break vs continue
Keep them straight: break leaves the loop entirely, while continue only ends the current pass and keeps looping.
Filter While Looping
Use continue to filter. Skip the values you do not care about and let only the interesting ones reach the rest of the body.
for n in nums:
if n < 0:
continue
total += nThey Work in while Too
break and continue are not just for for loops. They behave the same inside a while loop, controlling it the same way.
while True:
if done():
breakOnly the Inner Loop
In nested loops, break and continue affect just the innermost loop they sit in. Outer loops keep running normally.
for row in grid:
for cell in row:
if cell == 0:
breakEarly Exit from a Function
For leaving a whole function early, use return. It ends the function on the spot and hands back a value to the caller.
fn find(x: Int) -> Bool:
if x < 0:
return FalseGuard Clauses Read Well
Returning early on bad input, a guard clause, keeps the happy path clean and avoids deep nesting in your functions.
fn process(n: Int):
if n == 0:
return
work(n)Use Sparingly, Clearly
break, continue, and return are powerful but can hide flow. Reach for them when they make intent clearer, not just to save a line.
Quick Check
Inside a loop you hit a value you want to ignore, but you still want the loop to keep going. Which statement fits?
Recap
break leaves a loop, continue skips one pass, and return exits a function early. Use each to make your flow clearer. 🎯
Frequently asked questions
Is the “break, continue, and Early Exit” lesson free?
Yes — the full text of “break, continue, and Early Exit” is free to read here on the web, and the Mojo Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Mojo Academy course, upgrade to CoddyKit PRO.
What will I learn in “break, continue, and Early Exit”?
Control loops precisely. You practise Mojo Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Mojo Academy?
No prior experience is required. Mojo Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “break, continue, and Early Exit” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Mojo Academy lesson?
Yes. Every Mojo Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Making Choices with if/else
- Looping with while
- Iterating with for and range
- break, continue, and Early Exit