break, continue und vorzeitiger Abbruch
Steuern Sie Schleifen präzise.
break, continue und vorzeitiger Abbruch ist eine kostenlose Mojo Academy-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Mojo Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Mojo Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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. 🎯
Häufig gestellte Fragen
Ist die Lektion „break, continue und vorzeitiger Abbruch“ kostenlos?
Ja — der vollständige Text von „break, continue und vorzeitiger Abbruch“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Mojo Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Mojo Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „break, continue und vorzeitiger Abbruch“?
Steuern Sie Schleifen präzise. Du übst Mojo Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Mojo Academy zu starten?
Keine Vorkenntnisse erforderlich. Mojo Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.
Wie lange dauert die Lektion „break, continue und vorzeitiger Abbruch“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Mojo Academy-Lektion Code schreiben und ausführen?
Ja. Jede Mojo Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Mit if/else Entscheidungen treffen
- Schleifen mit while
- Mit for und range iterieren
- break, continue und vorzeitiger Abbruch