Loops in Dart
Understand to make use of loops in dart to run a block of code multiple times
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
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 keyword
The break statement is used to terminate the loop immediately.
Example:
void main() {
for (var i = 1; i <= 10; i++) {
if (i == 5) {
break;
}
print(i);
}
}
Output:
1
2
3
4
break keyword will break the loop immediately if the condition will be ture. Here the output will be only upto 4 because next value of i will terminate the loop.
continue Keyword
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) {
continue;
}
print(i);
}
}
Output:
1
2
3
4
6
7
8
9
10
Here, in this code when the value of i = 5, the code left to be executed will be skipped, that’s the reason why we don’t see 5.

