Dart do while Loop
The while loop in Dart first carries out a block of the statement before verifying the condition. The loop iterates further if the condition returns true. The sole distinction between it and the Dart while loop is that the do-while loop has a block of statements inside its body that run at least once.
Dart do-while loop Flowchart
Syntax
do {
// loop body
}while(condition);
In this case, the statement block inside the do while body will execute and then assess the supplied condition.
A condition is assessed as either true or false in a Boolean sense. The statements are run once again and the condition is verified if it returns true. The loop is terminated and control moves to the out-of-loop if it returns false.
Example
void main() {
int i = 10;
print("Dart do-while loop example");
do{
print(i);
i++;
}while(i<=20);
print("The loop is terminated");
}
Output
Dart do-while loop example
The value of i: 10
The value of i: 11
The value of i: 12
The value of i: 13
The value of i: 14
The value of i: 15
The value of i: 16
The value of i: 17
The value of i: 18
The value of i: 19
The value of i: 20
The loop is terminated
Explanation
The variable I has been initialized with the value 10 in the code above. The body of the do-while loop has two defined statements.
The statement printed the starting value of I and increased by 1 in the first repetition. We then verified the condition after the value of i increased to 11.
The requirement is that the value of I must either be less than or more than 20. The loop proceeded to the subsequent iteration when it matched the criterion. Up until the condition returned false, it printed the digits 10 through 20.