0Pricing
Mojo Academy · Lektion

Schleifen mit while

Wiederholen Sie etwas, bis sich eine Bedingung ändert.

Schleifen mit while ist eine kostenlose Mojo 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 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.

Repeat Until Done

Sometimes you must run code again and again. A while loop repeats its block as long as a condition stays true. 🔁

The while Shape

Write while, a condition, a colon, then an indented block. Mojo rechecks the condition before every pass through the body.

while count < 3:
    print(count)

Change the Condition Inside

The body must eventually make the condition false. Usually you update a counter so the loop progresses toward stopping.

var count = 0
while count < 3:
    print(count)
    count += 1

Watch for Infinite Loops

If the condition never turns false, the loop runs forever. An infinite loop usually means you forgot to update the variable it checks.

Condition Checked First

Mojo tests the condition before the first pass. If it is already false, the body never runs even once.

while False:
    print("never shown")

Accumulate a Result

Loops shine when building up a value. Keep a running total in a variable and add to it on each pass.

var total = 0
var i = 1
while i <= 5:
    total += i
    i += 1

Counting Down

A while loop can move in any direction. Start high and decrement each pass to count down toward a stopping point.

var n = 3
while n > 0:
    print(n)
    n -= 1

Loop on a Flag

The condition need not be a number. A boolean flag works too, letting other logic decide when the loop should end.

var running = True
while running:
    running = step()

Combine with if

Inside a while body you can branch with if. This lets each pass make its own decision based on the current state.

while i < 10:
    if i % 2 == 0:
        print(i)
    i += 1

Pick while When Count Is Unknown

Use while when you do not know in advance how many passes you need, such as reading until a value appears.

Update Before You Forget

A reliable habit: put the update step right where the loop must make progress, so the condition can truly change each pass.

Quick Check

You wrote a while loop but it runs forever and never stops. What is the most likely cause?

Recap

A while loop repeats while its condition is true, checking before each pass. Update the condition inside the body to avoid looping forever. 🎯

Häufig gestellte Fragen

Ist die Lektion „Schleifen mit while“ kostenlos?

Ja — der vollständige Text von „Schleifen mit while“ 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 „Schleifen mit while“?

Wiederholen Sie etwas, bis sich eine Bedingung ändert. 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 2 von 4.

Wie lange dauert die Lektion „Schleifen mit while“?

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

  1. Mit if/else Entscheidungen treffen
  2. Schleifen mit while
  3. Mit for und range iterieren
  4. break, continue und vorzeitiger Abbruch
← Zurück zu Mojo Academy