0Pricing
Zig Academy · Lektion

while-Schleifen und continue-Ausdrücke

Schleifen Sie mit Bedingungen und Schrittaktionen.

while-Schleifen und continue-Ausdrücke ist eine kostenlose Zig Academy-Lektion auf CoddyKit. Dies ist Lektion 2 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.

Repeat with while

A while loop repeats its body as long as a bool condition stays true. It checks the condition before each pass. 🔁

while (i < 5) {
    std.debug.print("{}\n", .{i});
    i += 1;
}

Checked Before Each Pass

Zig tests the condition first, so a while whose condition starts false runs zero times. The body is never guaranteed to execute.

Avoiding Infinite Loops

Each iteration should move toward making the condition false. Forget to update the variable and the loop spins forever.

The continue Expression

Zig adds a continue expression after a colon. It runs at the end of every iteration, the perfect place for your step update.

var i: usize = 0;
while (i < 5) : (i += 1) {
    work(i);
}

Why the continue Slot Helps

Putting the step in the continue slot keeps it visible at the top and guarantees it runs even after a continue statement skips ahead.

Skip with continue

The continue statement jumps to the next iteration immediately, skipping the rest of the body but still running the continue expression.

while (i < 10) : (i += 1) {
    if (i % 2 == 0) continue;
    odd_sum += i;
}

Stop Early with break

Use break to leave a while loop right away, even when the condition is still true. Control resumes after the loop.

while (true) {
    if (done()) break;
}

while as an Expression

A while can yield a value. break supplies the result on success, and an else clause provides the value when the loop finishes normally.

const found = while (i < n) : (i += 1) {
    if (match(i)) break i;
} else null;

Looping over Optionals

A while can keep unwrapping an optional, running while values are present and ending the moment it sees null.

while (it.next()) |item| {
    process(item);
}

Looping over Errors

Like if, a while can capture an error in an else branch when the unwrapped expression returns an error union instead of a value.

Infinite Loops on Purpose

Sometimes you want a deliberate forever loop. Writing while (true) with a break inside is the idiomatic event-loop pattern in Zig.

Quick Check

Where does Zig's continue expression run during a while loop?

Recap

You drove while: condition-first looping, the continue expression for clean steps, break and continue, and the value-returning expression form. 🎯

Häufig gestellte Fragen

Ist die Lektion „while-Schleifen und continue-Ausdrücke“ kostenlos?

Ja — der vollständige Text von „while-Schleifen und continue-Ausdrücke“ 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 „while-Schleifen und continue-Ausdrücke“?

Schleifen Sie mit Bedingungen und Schrittaktionen. 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 2 von 4.

Wie lange dauert die Lektion „while-Schleifen und continue-Ausdrücke“?

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