What are Loops?
Sometimes certain instructions in a program require repeated execution. Loops are an ideal way to do the same. A loop represents a set of instructions that must be repeated. In a loop’s context, a repetition is known as an iteration.
There are two types of loops:
- Indefinite
- Definite
For loop is a definite loop. The for loop executes the code block a specified number of times. It can be used to iterate over a fixed set of values, such as an array.
Moving on, let’s now discuss the indefinite loops. An indefinite loop is used when the number of iterations in a loop is indeterminate or unknown.
- For Loop: The for loop is used to iterate over a range of values. It consists of three parts: initialization, condition, and increment. The initialization part initializes the loop variable, the condition part is checked before each iteration, and the increment part updates the loop variable at the end of each iteration.
- While Loop: The while loop executes the instructions each time the condition specified evaluates to true. In other words, the loop evaluates the condition before the code block is executed.
- do…..while Loop: The do…while loop is similar to the while loop except that the do…while loop doesn’t evaluate the condition for the first time the loop executes.
Let us discuss the Loop Control System:
No | Control Statement & Description |
1 | break The break statement is used to take the control out of a construct. Using break in a loop causes the program to exit the loop. Following is an example of the break statement. |
2 | continue The continue statement skips the subsequent statements in the current iteration and takes the control back to the beginning of the loop. |
A label is simply an identifier followed by a colon (:) applied to a statement or a block of code. A label can be used with a break and continue to control the flow more precisely.
Line breaks are prohibited between the ‘continue’ or ‘break’ statement and its label name. Also, there should not be any other statement in between a label name and an associated loop.
Loops with Control Statements
This code demonstrates the implementation of different types of loops in Dart programming language, namely for loop, nested for loop, while loop, and do-while loop.
void main() {
print("## For Loop ##:");
for(var i=0;i<3;i++){
print("Item: ${i}");
}
print("\n## Nested For Loop ##:");
outerloop: // This is the label name
for (var i = 0; i < 2; i++) {
print("Innerloop: ${i}");
innerloop:
for (var j = 0; j < 2; j++) {
if (j > 3 ) break ;
// Quit the innermost loop
if (i == 1) break innerloop;
// Do the same thing
if (i == 2) break outerloop;
// Quit the outer loop
print("Innerloop: ${j}");
}
}
print("\n## While Loop ##:");
var i=0;
while(i<=3){
print("Item: ${i}");
i++;
}
print("\n## Do ... While Loop ##:");
i=0;
do{
i++;
print("Item: ${i}");
}while(i<=3);
}
The following output is displayed on successful execution of the above code.
## For Loop ##:
Item: 0
Item: 1
Item: 2
## Nested For Loop ##:
Innerloop: 0
Innerloop: 0
Innerloop: 1
Innerloop: 1
## While Loop ##:
Item: 0
Item: 1
Item: 2
Item: 3
## Do ... While Loop ##:
Item: 1
Item: 2
Item: 3
Item: 4
The first for loop iterates from 0 to 2 and prints the value of i for each iteration. The nested for loop demonstrates the use of labels in Dart. The outer loop iterates from 0 to 1, and the inner loop iterates from 0 to 1. The if statements within the inner loop use the break keyword to quit the loop under certain conditions. The first if statement will break the inner loop if j is greater than 3. The second if statement will break the inner loop if i is equal to 1. The third if statement will break the outer loop if i is equal to 2. The while loop starts with i=0 and continues iterating until i becomes greater than 3. For each iteration, the value of i is printed. The do-while loop is similar to the while loop, but it guarantees that the loop body will execute at least once, regardless of the initial value of the loop variable.