🛠 Flutter world will be ready by the end of September, 2024 🚀🔥
Learn Dart Programming
Dart Basics
Loops in Dart

Loops in Dart

Loops are used to execute a set of statements repeatedly until a particular condition is satisfied. In Dart, we have the following types of loops:

  • for loop
  • while loop
  • do-while loop
  • for-in loop

for loop

The for loop is used to execute a set of statements a specific number of times.

Syntax:

for (initialization; condition; increment/decrement) {  
    // statements to be executed  
}  

Example:

void main() {  
    for (var i = 1; i <= 10; i++) {  
        print(i);  
    }  
}  

Output:

1
2
3
4
5
6
7
8
9
10

while loop

The while loop is used to execute a set of statements until a particular condition is satisfied.

Syntax:

while (condition) {  
    // statements to be executed  
}  

Example:

 
void main() {  
    var i = 1;  
    while (i <= 10) {  
        print(i);  
        i++;  
    }  
}  

Output:

 
1
2
3
4
5
6
7
8
9
10

do-while loop

The do-while loop is used to execute a set of statements at least once and then repeatedly executes until a particular condition is satisfied.

Syntax:

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

Example:

void main() {  
    var i = 1;  
    do {  
        print(i);  
        i++;  
    } while (i <= 10);  
}  

Output:

1
2
3
4
5
6
7
8
9
10

for-in loop

The for-in loop is used to iterate through the elements of lists, sets, or maps.

Syntax:

for (var item in collection) {  
    // statements to be executed  
}  

Example:

 
void main() {  
    var numbers = [1, 2, 3];  
    for (var n in numbers) {  
        print(n);  
    }  
}  

Output:

 
1
2
3

break and continue

The break statement is used to terminate the loop immediately. The continue statement is used to skip the current iteration of the loop and the control flow will continue with the next iteration.

Example:

void main() {  
    for (var i = 1; i <= 10; i++) {  
        if (i == 5) {  
            break;  
        }  
        print(i);  
    }  
}  

Output:

1
2
3
4

Example:

void main() {  
    for (var i = 1; i <= 10; i++) {  
        if (i == 5) {  
            continue;  
        }  
        print(i);  
    }  
}  

Output:

1
2
3
4
6
7
8
9
10

forEach loop

The forEach loop is used to iterate through the elements of lists, sets, or maps.

Syntax:

collection.forEach((item) {  
    // statements to be executed  
});  

Example:

void main() {  
    var numbers = [1, 2, 3];  
    numbers.forEach((n) => print(n));  
}  

Output:

 
1
2
3

Nested loops

A loop inside another loop is called a nested loop. The inner loop will be executed one time for each iteration of the outer loop.

Example:

void main() {  
    for (var i = 1; i <= 3; i++) {  
        for (var j = 1; j <= 3; j++) {  
            print("$i $j");  
        }  
    }  
}  

Output:

 
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3

Infinite loops

A loop that never ends is called an infinite loop. If the condition of the while loop is always true, then the loop will never end.

Example:

 
void main() {  
    while (true) {  
        print("Hello World");  
    }  
}  

Output:

 
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World 
.
.
.

Loop control statements

Loop control statements change the execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed.

Dart supports the following control statements:

  • break statement
  • continue statement

break statement

The break statement is used to terminate the loop immediately. The control of the program flows to the statement immediately after the body of the loop.

Syntax:

break;  

Example:

 
void main() {  
    for (var i = 1; i <= 10; i++) {  
        if (i == 5) {  
            break;  
        }  
        print(i);  
    }  
}  

Output:

 
1
2
3
4

continue statement

The continue statement is used to skip the current iteration of the loop and the control flow will continue with the next iteration.

Syntax:

continue;  

Example:

 
void main() {  
    for (var i = 1; i <= 10; i++) {  
        if (i == 5) {  
            continue;  
        }  
        print(i);  
    }  
}  

Output:

 
1
2
3
4
6
7
8
9
10

Conclusion

In this tutorial, we have learned about loops in Dart with the help of examples.

Quiz

  1. What is the output of the following code?
void main() {  
    var i = 1;  
    while (i <= 10) {  
        print(i);  
        i++;  
    }  
}  

a) 1 2 3 4 5 6 7 8 9 10

b) 1 2 3 4 5 6 7 8 9

c) 1 2 3 4 5 6 7 8 9 10 11

d) 1 2 3 4 5 6 7 8 9 10 11 12

View Answer

Answer: a

Explanation: The while loop is used to execute a set of statements until a particular condition is satisfied.

  1. What is the output of the following code?
void main() {  
    var i = 1;  
    do {  
        print(i);  
        i++;  
    } while (i <= 10);  
}  

a) 1 2 3 4 5 6 7 8 9 10

b) 1 2 3 4 5 6 7 8 9

c) 1 2 3 4 5 6 7 8 9 10 11

d) 1 2 3 4 5 6 7 8 9 10 11 12

View Answer

Answer: a

Explanation: The do-while loop is used to execute a set of statements at least once and then repeatedly executes until a particular condition is satisfied.

  1. What is the output of the following code?
void main() {  
    var numbers = [1, 2, 3];  
    for (var n in numbers) {  
        print(n);  
    }  
}  

a) 1 2 3

b) 1 2 3 4

c) 1 2 3 4 5

d) 1 2 3 4 5 6

View Answer

Answer: a

Explanation: The for-in loop is used to iterate through the elements of lists, sets, or maps.

  1. What is the output of the following code?
 
void main() {  
    var numbers = [1, 2, 3];  
    numbers.forEach((n) => print(n));  
}  

a) 1 2 3

b) 1 2 3 4

c) 1 2 3 4 5

d) 1 2 3 4 5 6

View Answer

Answer: a

Explanation: The forEach loop is used to iterate through the elements of lists, sets, or maps.

  1. What is the output of the following code?
 
void main() {  
    for (var i = 1; i <= 3; i++) {  
        for (var j = 1; j <= 3; j++) {  
            print("$i $j");  
        }  
    }  
}  

a) 1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3

b) 1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3 4

c) 1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3 4 4

d) 1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3 4 4 4

View Answer

Answer: a

Explanation: A loop inside another loop is called a nested loop. The inner loop will be executed one time for each iteration of the outer loop.

  1. What is the output of the following code?
 
void main() {  
    while (true) {  
        print("Hello World");  
    }  
}  

a) Hello World

b) Hello World Hello World

c) Hello World Hello World Hello World

d) Hello World Hello World Hello World Hello World ... infinite times

View Answer

Answer: d

Explanation: A loop that never ends is called an infinite loop. If the condition of the while loop is always true, then the loop will never end.

  1. What is the output of the following code?
 
void main() {  
    for (var i = 1; i <= 10; i++) {  
        if (i == 5) {  
            break;  
        }  
        print(i);  
    }  
}  

a) 1 2 3 4

b) 1 2 3 4 5

c) 1 2 3 4 5 6

d) 1 2 3 4 5 6 7

View Answer

Answer: a

Explanation: The break statement is used to terminate the loop immediately. The control of the program flows to the statement immediately after the body of the loop.

  1. What is the output of the following code?
 
void main() {  
    for (var i = 1; i <= 10; i++) {  
        if (i == 5) {  
            continue;  
        }  
        print(i);  
    }  
}  

a) 1 2 3 4 6 7 8 9 10

b) 1 2 3 4 5 6 7 8 9 10

c) 1 2 3 4 5 6 7 8 9 10 11

d) 1 2 3 4 5 6 7 8 9 10 11 12

View Answer

Answer: a

Explanation: The continue statement is used to skip the current iteration of the loop and the control flow will continue with the next iteration.

  1. What is the output of the following code?
 
void main() {  
    var i = 1;  
    while (i <= 10) {  
        print(i);  
        i++;  
    }  
}  

a) 1 2 3 4 5 6 7 8 9 10

b) 1 2 3 4 5 6 7 8 9

c) 1 2 3 4 5 6 7 8 9 10 11

d) 1 2 3 4 5 6 7 8 9 10 11 12

View Answer

Answer: a

Explanation: The while loop is used to execute a set of statements until a particular condition is satisfied.

  1. What is the output of the following code?
 
void main() {  
    var i = 1;  
    do {  
        print(i);  
        i++;  
    } while (i <= 10);  
}  

a) 1 2 3 4 5 6 7 8 9 10

b) 1 2 3 4 5 6 7 8 9

c) 1 2 3 4 5 6 7 8 9 10 11

d) 1 2 3 4 5 6 7 8 9 10 11 12

View Answer

Answer: a

Explanation: The do-while loop is used to execute a set of statements at least once and then repeatedly executes until a particular condition is satisfied.