break, continue y salida anticipada
Controle los bucles con precisión.
break, continue y salida anticipada es una lección gratuita de Mojo Academy en CoddyKit. Esta es la lección 4 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Mojo Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Mojo Academy incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
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. 🎯
Preguntas frecuentes
¿La lección «break, continue y salida anticipada» es gratis?
Sí — el texto completo de «break, continue y salida anticipada» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Mojo Academy, actualiza a CoddyKit PRO. El curso de Mojo Academy incluye 4 lecciones en total.
¿Qué aprenderé en «break, continue y salida anticipada»?
Controle los bucles con precisión. Practicas Mojo Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar Mojo Academy?
No se requiere experiencia previa. Mojo Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 4 de 4.
¿Cuánto tiempo toma la lección «break, continue y salida anticipada»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de Mojo Academy?
Sí. Cada lección de Mojo Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- Tomar decisiones con if/else
- Repetir con while
- Iterar con for y range
- break, continue y salida anticipada