Lesson contents

Repeat with a while loop

A while loop repeats a step while its condition is true.

Simple example

Start1 → 3
Result1 2 3
lesson.dart
void main() {
    var number = 1;
    while (number <= 3) {
        print(number);
        number += 1;
    }
}
Output
1
2
3
Advertisements

Everyday examples

A ticket roll starts at 21. Show the next three ticket numbers, starting with 21.

Start21 → 23
Result21 22 23
lesson.dart
void main() {
    var ticket = 21;
    while (ticket <= 23) {
        print(ticket);
        ticket += 1;
    }
}
Output
21
22
23
Run the example
Terminal
dart run lesson.dart
Official documentation