In this chapter, we will learn about dart while and do while loop and how they are used with the help of examples.
while Loop
The while loop executes the block of code each time the condition specified results to true.
The syntax of while loop is:
while (expression/condition) {
// statements that are executed if condition/expression is true
}
Here,
A while loop evaluates the condition inside the parenthesis ().
If the condition results in true, the code inside the while loop executes.
The condition is evaluated again.
This process continues until the condition is false.
When the condition results in false, the loop terminates.
Example: Display the Text 5 Times
void main()
{
int num = 1;
while (num <= 5)
{
print('Hello there!');
++num;
}
}
Output
Hello there!
Hello there!
Hello there!
Hello there!
Hello there!
In the above example,
- The initial value of variable i is 1. Then the condition i <= 5 is checked, which results in true. Hence the text ‘Hello there!’ is printed. The value of i increases by 1 (++i).
- During the second iteration, the condition i <= 5 is checked again. This condition results in true. Hence the text ‘Hello there!’ is printed. The value of i increases by 1 (++i).
This process continues until the value of variable i increases to 6. Then condition i <= 5 results in false. And the loop terminates.
Dart do…while Loop
The syntax of do…while loop is:
do {
// body of loop
} while(condition);
Here,
The do…while loop executes a block of statements first and then evaluates the condition.
If the condition results in true, the body of the loop inside the do statement executes again.
The condition is evaluated once again.
If the condition results in true, the body of the loop inside the do statement executes again.
This process continues until the condition results in false. Then the loop terminates.
Example: do…while loop
void main()
{
int num = 1;
do {
print('Hello there!');
++num;
} while (num <=5);
}
Output
Hello there!
Hello there!
Hello there!
Hello there!
Hello there!
In the above example,
- The initial value of the num variable is 1. The body inside the loop executes. And ‘Hello there!’ is printed. The num variable increments by 1 (++num).
Then the condition is checked. Here, num <= 5 results in true.
- During the second iteration, the value of num variable is 2. The body inside the loop is executed. And ‘Hello there!’ is printed. The num variable increments by 1 (++num).
This process continues until the value of num is 6. Then the condition num <= 5 results in false. And the loop terminates.
do…while and while Loop
do…while loop is similar to the while loop. The difference is that in the dart while loop, the condition is evaluated first. And in do…while loop, the body of loop executes at least once.
For example,
void main()
{
int num = 5;
do {
print('Hello there!');
++num;
} while (num <=5);
}
Output
Hello there!
Here, the value of the num variable is 6. The body inside the loop is executed. And ‘Hello there!’ is printed. The num variable increments by 1 (++num).
Then the condition is checked. Here, num <= 5 results in false. And the loop terminates.
Infinite while Loop
If the condition of a loop is always true, the loop runs for infinite times (until the memory is full). This is known as an infinite loop.
Here is an example of an infinite while loop.
void main() {
// infinite while loop
while(true){
print('Hello there!');
}
}
Here is an example of an infinite do…while loop.
void main() {
// infinite do...while loop
const count = 0;
do {
print('Hello there!');
} while(count == 0);
}
for Vs while Loop
while Loop | for Loop |
A while and do…while loop is useful when the number of iterations are unclear. | A for loop is useful when the number of iterations is known. |
For example: while (condition) { // statements } | For example: for (let num = 1; num <=5; ++num) { // statements } |