Dart Do-While Loop

Dart Do-While loop statement is same as that of while loop except that the statements in loop are executed first, and then the condition is checked.

In this tutorial, we will learn the syntax to write do-while loop statement in Dart programming language, and how to use do-while loop with the help of example programs.

Syntax

The syntax of do-while loop is

do {
  // statement(s)
} while (condition);
ADVERTISEMENT

Examples

In the following program, we write a do-while loop statement that prints 'Hello World' five times.

main.dart

void main() {
  var i = 0;
  do {
    print('Hello World');
    i++;
  } while (i < 5);
}

Output

Hello World
Hello World
Hello World
Hello World
Hello World

Now, let us take a condition that is always false. Since, do-while loop executes the block inside it at least one, 'Hello World' would be printed at least once.

main.dart

void main() {
  var i = 0;
  do {
    print('Hello World');
    i++;
  } while (false);
}

Output

Hello World

Conclusion

In this Dart Tutorial, we learned what a do-while loop is, its syntax, and its working with examples.