thumbnail

While Loop in Dart

Published on 29 March 2020
Last Updated on 29 March 2020

While loop is one of the control flow statements in Dart.

Use of While Loop in Dart

While loop is used for evaluating a condition before loop executes.

while loop has following syntax:

while (condition){
    statement to be executed by the loop.
}

Program using while loop

Given below is a program that demonstrates how While loops can be used in Dart.

// nested for loop
main(List<String> args) {
  var condition = true;
  var count = 0;
  while (condition) {
    count++;
    print('condition is: ${condition}, count is: ${count}, executing the loop');
    if (count == 3) {
      // make condition false when count reaches 3
      condition = false;
    }
  }
}

Above program produces following output:

condition is: true, count is: 1, executing the loop
condition is: true, count is: 2, executing the loop
condition is: true, count is: 3, executing the loop

As shown in above program, while loop can be used as for loop by defining a custom condition and a count outside the loop.