0Pricing
Dart Academy · Lesson

while and do-while Loops

Loop until a condition changes.

while and do-while Loops is a free Dart 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 Dart Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

When Count Is Unknown

Sometimes you do not know how many passes you need ahead of time. A while loop repeats as long as a condition stays true. 🔄

The while Shape

A while loop checks its condition first, then runs the body. If the condition is false at the start, the body never runs at all.

while (condition) {
  // body
}

A Simple Counter

You manage the counter yourself: declare it before the loop and update it inside the body so the condition can eventually fail.

var i = 0;
while (i < 3) {
  print(i);
  i++;
}

Beware Infinite Loops

If the condition never becomes false, your program loops forever. Always change something inside the body that moves toward stopping.

while (true) {
  // never ends without a break
}

Condition First

The key trait of while is that it tests before the body. With i starting at 5 and a limit of 3, the body is skipped entirely.

var i = 5;
while (i < 3) {
  print('skipped');
}

Meet do-while

A do-while loop flips the order: it runs the body once, then checks the condition. So the body always runs at least one time.

do {
  // body
} while (condition);

Guaranteed One Pass

Even when the condition is false from the start, do-while still executes once before testing. Notice the semicolon after the condition.

var i = 9;
do {
  print(i);
} while (i < 3);

Reading User Input

The do-while shines for menus and prompts: you always show the prompt once, then repeat only if the input is invalid.

do {
  // ask once, repeat if bad
} while (!valid);

while vs do-while

The real difference is the first run: while may skip the body, but do-while always runs it at least once. Pick by that rule.

Accumulating Results

Loops often build up a value over time. Here we add numbers into a total until the counter reaches its limit.

var total = 0, i = 1;
while (i <= 4) {
  total += i;
  i++;
}

Looping on a Flag

A while loop can run on a boolean flag instead of a counter, repeating until some event flips the flag to false.

var running = true;
while (running) {
  running = false;
}

Quick Check

You need a loop whose body runs at least once even if the condition is false. Which fits?

Recap

You met while and do-while loops. While tests first and may skip; do-while runs once then tests. Always move toward stopping. 🎯

Frequently asked questions

Is the “while and do-while Loops” lesson free?

Yes — the full text of “while and do-while Loops” is free to read here on the web, and the Dart 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 Dart Academy course, upgrade to CoddyKit PRO.

What will I learn in “while and do-while Loops”?

Loop until a condition changes. You practise Dart 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 Dart Academy?

No prior experience is required. Dart 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 “while and do-while Loops” 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 Dart Academy lesson?

Yes. Every Dart 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. The Classic for Loop
  2. while and do-while Loops
  3. for-in Over Collections
  4. break, continue, and Labels
← Back to Dart Academy