In this chapter, we will learn about the loops and dart for loops.
Loops
In programming, loops are used to iterate/repeat a block of code.
For example, if we want to add a number to the previous value 100 times or if we want to show a particular message 100 times, then we can use a loop.
In Dart, we have the following loop statements.
- Dart for loop
- Dart for…in loop
- Dart while loop
- do while loop
Dart for loop
The syntax of the for loop is:
for (initialExpression; condition; updateExpression) {
// for loop statements
}
Here,
The initialExpression sets the variable and executes only once.
The condition is evaluated.
- If the condition is false, the for loop is terminated.
- If the condition is true, the block of code inside of the for loop is executed.
The updateExpression updates the value of initialExpression when the condition is true.
The condition is evaluated again. This process continues until the condition is false.
Example: Display a text 6 times
void main() {
for (int num = 1; num <= 6; num++) {
print('Hi, there!');
}
}
Output
Hi, there!
Hi, there!
Hi, there!
Hi, there!
Hi, there!
Hi, there!
Here,
- In the first iteration, the num variable has the initial value of 1. The condition num <= 6 is true. Hence, “ Hi, there” is printed. The num variable is increased by 1 (num++).
- In the second iteration, the num variable has a value of 2. The condition num <= 6 is true. Hence, “ Hi, there” is printed.
This process continues for seven iterations. During the seventh iteration, the value of num will be 7. The condition num <= 6 will be false. And the loop terminates.
Working of Dart for Loop
To see the for loop in work, let’s print the value of the num variable.
void main() {
for (int num = 1; num <= 6; num++) {
print('Hi, there!');
print(num);
}
}
Output
Hi, there!
1
Hi, there!
2
Hi, there!
3
Hi, there!
4
Hi, there!
5
Hi, there!
6
Example: Display only the Even Numbers Between 10 and 20
void main() {
for (int num = 10; num <= 20; num++) {
if(num % 2 == 0) {
print(num);
}
}
}
Output
10
12
14
16
18
20
Here,
- During the first iteration, the num variable has an initial value of 10. The condition num <= 20 results in true. Then the program control flow moves inside the for loop. Inside the for loop, the condition num % 2 == 0 is checked. 10 % 2 results in 0. Hence the if condition is true and the number is printed. And the number is increased by 1 (num++).
- During the second iteration, the value of num is 11. And the condition num <= 20 results in true. Inside the for loop, the condition num % 2 is checked, which results in false. And the num variable is not printed.
This process continues until the value of the num variable reaches 21. Then the condition num <= 20 results in false, and the loop terminates.
Infinite for 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 for loop.
void main() {
for (int num = 1; num > 0; num++) {
// statements
}
}
void main() {
for (;;) {
// statements
}
}
In both above cases, the condition is always true. Hence the loop runs for infinite number of times.
In the next tutorial, we will learn about the for…in loop.