← All languages
Dart function of the day
Random

do-while

Execute a block of code at least once, then repeat while a condition is true.

Description

The do-while loop executes the loop body first and then evaluates the condition. If the condition is true, the loop repeats; if false, the loop terminates. This guarantees the body runs at least once, unlike a standard while loop.

Do-while is useful when you need to execute an action before checking whether to continue, such as displaying a menu and then checking user input, or attempting an operation before checking for success.

Like while loops, do-while supports break to exit the loop early and continue to skip to the condition check. The condition is evaluated after every complete execution of the loop body.

Example

var number = 1;
do {
  print(number);
  number *= 2;
} while (number < 100);
// Prints: 1, 2, 4, 8, 16, 32, 64

Reference