0Pricing
Zig Academy · Lektion

break, continue und markierte Schleifen

Steuern Sie Schleifen mit Labels präzise.

break, continue und markierte Schleifen ist eine kostenlose Zig 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 Zig Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Zig Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

Two Loop Controls

Inside any loop, break exits the loop entirely while continue jumps to the next iteration. They give you fine control over flow. 🎛️

break Leaves Now

When break runs, the loop stops immediately and execution resumes on the line right after the loop. Nothing else in the body runs.

for (items) |x| {
    if (x == 0) break;
    use(x);
}

continue Skips Ahead

A continue abandons the rest of the current pass and moves on to the next iteration, leaving the loop itself running.

for (items) |x| {
    if (x < 0) continue;
    total += x;
}

They Target the Innermost Loop

By default break and continue affect only the innermost loop around them. An outer loop keeps running as if nothing happened.

Naming a Loop

You can attach a label to a loop with a name and a colon. That label gives you a handle to target it from deeper inside.

outer: for (rows) |row| {
    for (row) |cell| {
        check(cell);
    }
}

break to a Label

Write break :outer to exit a named outer loop directly from within a nested loop. One statement unwinds both levels.

outer: for (rows) |row| {
    for (row) |cell| {
        if (cell == target) break :outer;
    }
}

continue to a Label

Use continue :outer to skip to the next iteration of a labeled outer loop, abandoning the rest of the inner loop instantly.

Labels Replace Flags

Before labels you might set a found flag and check it after each loop. Labeled break removes that boilerplate and reads more clearly.

Labeled break Returns a Value

A labeled loop can also be an expression. break :outer value hands that value back as the result of the whole labeled loop.

const hit = outer: for (rows) |row| {
    for (row) |c| {
        if (c == target) break :outer c;
    }
} else 0;

Labeled Blocks Too

Labels are not just for loops. A plain block can be labeled, and break with a value turns that block into an expression.

const x = blk: {
    const t = compute();
    break :blk t * 2;
};

Keep Nesting Shallow

Labeled control is powerful, but deeply nested loops hurt readability. Reach for labels when they clarify, not to excuse tangled code.

Quick Check

From an inner loop you want to exit the outer loop named outer. Which statement does it?

Recap

You mastered break and continue: innermost by default, labeled loops for nesting, value-returning labeled breaks, and labeled blocks. 🎯

Häufig gestellte Fragen

Ist die Lektion „break, continue und markierte Schleifen“ kostenlos?

Ja — der vollständige Text von „break, continue und markierte Schleifen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Zig Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Zig Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „break, continue und markierte Schleifen“?

Steuern Sie Schleifen mit Labels präzise. Du übst Zig 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 Zig Academy zu starten?

Keine Vorkenntnisse erforderlich. Zig 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 markierte Schleifen“?

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 Zig Academy-Lektion Code schreiben und ausführen?

Ja. Jede Zig 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

  1. if als Anweisung und Ausdruck
  2. while-Schleifen und continue-Ausdrücke
  3. for-Schleifen über Bereiche und Elemente
  4. break, continue und markierte Schleifen
← Zurück zu Zig Academy