Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

Dart Tutorial

Dart HOME Dart Intro Dart Get Started Dart Syntax Dart Output Dart Comments Dart Variables Dart final and const Dart Data Types Dart Numbers Dart Strings Dart String Interpolation Dart String Methods Dart Booleans Dart Type Conversion Dart Null Safety Dart late Dart Operators Dart Arithmetic Dart Assignment Dart Comparison Dart Logical Dart Type Test Dart Null Operators Dart Cascade Dart If...Else Dart Switch Dart While Loop Dart For Loop Dart Break/Continue

Dart Collections

Dart Lists Dart Access List Items Dart Change List Items Dart Loop Lists Dart List Methods Dart Sets Dart Maps Dart Access Map Items Dart Loop Maps Dart Collection if/for Dart Spread Operator

Dart Functions

Dart Functions Dart Parameters Dart Named Parameters Dart Return Values Dart Arrow Functions Dart Anonymous Functions Dart Closures Dart Scope Dart Recursion

Dart Classes

Dart OOP Dart Classes/Objects Dart Constructors Dart Named Constructors Dart Factory Constructors Dart Getters and Setters Dart Static Members Dart Inheritance Dart Abstract Classes Dart Interfaces Dart Mixins Dart Extension Methods Dart Enums Dart Generics

Dart 3 Features

Dart Records Dart Patterns Dart Switch Expressions Dart Sealed Classes Dart Class Modifiers

Dart Errors

Dart Exceptions Dart Custom Exceptions

Dart Async

Dart Async Intro Dart Futures Dart async/await Dart Streams Dart Isolates

Dart Libraries

Dart Imports Dart Packages Dart Math Dart Dates Dart JSON Dart RegExp Dart User Input Dart Files Dart Testing

Dart Examples

Dart Examples Dart Exercises


Dart While Loop


Share

Loops

A loop runs the same block of code many times.

Loops save time, and they make code shorter and easier to read.

Dart has these loops:

  • while and do-while, covered in this chapter
  • for and for-in, covered in the For Loop chapter

Dart While Loop

The while loop repeats a block of code as long as a condition is true:

while (condition) {
  // code block to run
}

In this example, the loop runs as long as i is less than 5:

Example

void main() {
  var i = 0;
  while (i < 5) {
    print(i);
    i++;
  }
}
Try it Yourself »

Result:

0
1
2
3
4

This is how it works:

  • Dart tests the condition i < 5 before each run of the loop
  • If the condition is true, Dart runs the code block
  • i++ adds 1 to i each time
  • When i becomes 5, the condition is false and the loop stops

We use var and not final for i, because its value changes.

Note: Do not forget to change the variable that the condition uses.

If the condition never becomes false, the loop never ends.

This is called an infinite loop.



More While Examples

A while loop can count down too:

Example

void main() {
  var countdown = 3;
  while (countdown > 0) {
    print(countdown);
    countdown--;
  }
  print('Happy New Year!');
}
Try it Yourself »

Result:

3
2
1
Happy New Year!

A while loop is useful when you do not know how many times the loop must run.

This example doubles a savings amount until it reaches at least 1000:

Example

void main() {
  var savings = 100;
  var years = 0;
  while (savings < 1000) {
    savings = savings * 2;
    years++;
  }
  print('After $years years, the savings are $savings');
}
Try it Yourself »

Result:

After 4 years, the savings are 1600

The Do-While Loop

The do-while loop runs the code block first, and then tests the condition:

do {
  // code block to run
} while (condition);

Note the semicolon ; after the condition.

Example

void main() {
  var i = 0;
  do {
    print(i);
    i++;
  } while (i < 5);
}
Try it Yourself »

Result:

0
1
2
3
4

While or Do-While?

A do-while loop always runs at least once, even when the condition is false from the start.

Here the condition i < 5 is false, but the code block still runs one time:

Example

void main() {
  var i = 10;
  do {
    print('i is $i');
    i++;
  } while (i < 5);
}
Try it Yourself »

Result:

i is 10

A while loop with the same condition does not run at all, so this program prints only "Done":

Example

void main() {
  var i = 10;
  while (i < 5) {
    print('i is $i');
    i++;
  }
  print('Done');
}
Try it Yourself »

Result:

Done

Stop or Skip

To stop a loop early, or to skip one run of a loop, use break and continue.

You will learn about them in the Break and Continue chapter.



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.