Skip to main content
The do-while loop is an exit-controlled iteration statement that executes a block of code once before evaluating a boolean condition. Unlike the standard while loop, the do-while loop guarantees that the loop body runs at least one time, regardless of whether the condition is initially true or false.

Syntax

do {
  // Statement(s) to be executed
} while (condition);

Parameters

  • Statement(s): A block of code enclosed in braces {} that performs the loop’s operations.
  • Condition: An expression that evaluates to a bool. This is evaluated after the execution of the statement block.

Execution Logic

  1. Body Execution: The program enters the do block and executes the statements contained within it.
  2. Condition Evaluation: Upon reaching the while keyword, the specified condition is evaluated.
  3. Branching:
    • If the condition evaluates to true, control returns to the beginning of the do block, and the process repeats.
    • If the condition evaluates to false, the loop terminates, and control passes to the next statement following the loop.

Example

The following example demonstrates the syntax and execution flow. Note that the variable count is incremented inside the loop, eventually causing the condition count < 3 to evaluate to false.
void main() {
  int count = 0;

  do {
    print('Current count: $count');
    count++;
  } while (count < 3);
}
Output:
Current count: 0
Current count: 1
Current count: 2

False Condition Behavior

Because the condition is evaluated at the end of the iteration, the body executes once even if the condition is false at the start.
void main() {
  bool isReady = false;

  do {
    print('This executes exactly once.');
  } while (isReady);
}

Loop Control Statements

The do-while loop supports standard Dart control flow statements:
  • break: Immediately terminates the loop, bypassing the condition check.
  • continue: Skips the remainder of the current iteration and jumps directly to the condition evaluation at the end of the loop.
Master Dart with Deep Grasping Methodology!Learn More