0Pricing
Mojo Academy · Lesson

Looping with while

Repeat until a condition changes.

Looping with while is a free Mojo Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Mojo Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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. 🎯

Frequently asked questions

Is the “Looping with while” lesson free?

Yes — the full text of “Looping with while” is free to read here on the web, and the Mojo Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Mojo Academy course, upgrade to CoddyKit PRO.

What will I learn in “Looping with while”?

Repeat until a condition changes. You practise Mojo Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Mojo Academy?

No prior experience is required. Mojo Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Looping with while” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Mojo Academy lesson?

Yes. Every Mojo Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Making Choices with if/else
  2. Looping with while
  3. Iterating with for and range
  4. break, continue, and Early Exit
← Back to Mojo Academy