# while and do-while in Dart Programming

Dart ProgrammingServer Side ProgrammingProgramming

While and do-while loops are also present in Dart's arsenal. They are quite similar in terms of syntax and functionality to the C's while and do-while loops.

## While loop

A while loop is an indefinite loop that can be modified to run for a finite number of iterations based on the condition we provide.

## Syntax

while(condition){
// do this
}

### Example

Consider the example shown below −

Live Demo

void main() {
var age = 6;
while(age < 10){
print("age is now $age"); age++; } } ### Output age is now 6 age is now 7 age is now 8 age is now 9 It should be noted that if we forget to remove the increment condition, then we will be in an infinite loop as the condition inside the parentheses after the while keyword will always be true, hence it can cause our program to behave abnormally. ## do-while loop The do-while loop does one thing slightly different from the basic while loop and that is, even when a condition inside the parentheses evaluates to false, it will run one iteration for sure. ### Syntax do{ // do this }while( condition ) ### Example Consider the example shown below − Live Demo void main() { var age = 6; do{ print("age is now$age");
}while(age++ < 10);
}

### Output

age is now 6
age is now 7
age is now 8
age is now 9
age is now 10

It should be noted that even if the condition inside the parentheses after the while keyword isn't true, the loop will run for on iteration for sure.

### Example

Consider the example shown below −

Live Demo

void main() {
var age = 6;
do{
print("age is now \$age");
}while(age++ > 10);
}

## Output

age is now 6
Published on 24-May-2021 12:18:27