Do While Loop in Dart

Published on 29 March 2020
Last Updated on 29 March 2020

Do while is one of the control flow statements in Dart.

Do while loop evaluates the condition after loop has executed at least one time. After condition is evaluated for the first time then condition is evaluated for subsequent execution as well.

When a do while loop is used, code will be executed at least once.

// nested for loop
main(List<String> args) {
  var counter = 0;
  do {
    print('counter is: ${counter}');
  } while (counter < 5);

Above program produces following output:

counter is: 0
counter is: 1
counter is: 2
counter is: 3
counter is: 4

As shown in above program, when a do-while loop is used. Statements defined inside do-while loop are executed at least once before condition is evaluated.

You can also use break and continue statements inside do-while block to end the loop or jump outside the loop conditionally.