Dart Loops
A code block can be repeatedly run using a dart loop for a predetermined number of iterations or until it meets the predetermined condition. An integral part of any programming language are loops. It is used to repeatedly execute actions on a Dart iterable, such as a list, map, etc. Control statements and the loop’s body are the two possible components. The loop’s primary goal is to execute the function several times. The following types of loops are supported by Dart.
- Dart for loop
- Dart for…in loop
- Dart while loop
- Dart do-while loop
Here is a quick overview of the dart loops that we outline.
Dart for loop
When we know how many times a block of code will run, we use the for loop. That is identical to the C for loop. Below is the syntax.
Syntax
for(Initialization; condition; incr/decr) {
// loop body
}
The beginning value is where the loop iterates from. It just runs once.
A test expression serves as the condition, which is verified at the end of each iteration. The for loop will keep running until the specified condition returns false.
The counter to raise or lower the value is the incr/decr.
Example
void main()
{
int num = 1;
for(num; num<=10; num++) //for loop to print 1-10 numbers
{
print(num); //to print the number
}
}
Output
1
2
3
4
5
6
7
8
9
10
Dart for… in Loop
The for loop and the for.in loop differ slightly. It iterates each element one at a time and only accepts a dart object or expression as an iterator. The element’s value is bound to var, which is both valid and accessible for the body of the loop. The loop will keep running until the iterator has no more elements. Below is the syntax.
Syntax
for (var in expression) {
//statement(s)
}
Example
void main()
{
var list1 = [10,20,30,40,50];
for(var i in list1) //for..in loop to print list element
{
print(i); //to print the number
}
}
Output
10
20
30
40
50
Dart while loop
Until the specified expression is false, a block of code is executed by the while loop. It is more advantageous if we are unsure of the total number of executions. Below is the syntax.
Syntax
while(condition) {
// loop body
}
Example
void main()
{
var a = 1;
var maxnum = 10;
while(a
Output
1
2
3
4
5
6
7
8
9
Dart do…while Loop
The sole distinction between the while and do…while loops is that the latter checks the supplied condition after executing the loop statement. Below is the syntax.
Syntax
do {
// loop body
} while(condition);
Example
void main()
{
var a = 1;
var maxnum = 10;
do
{
print("The value is: ${a}");
a = a+1;
}while(a
Output
The value is: 1
The value is: 2
The value is: 3
The value is: 4
The value is: 5
The value is: 6
The value is: 7
The value is: 8
The value is: 9
Selection of the loop
For a coder, choosing a loop is a somewhat challenging process. Selecting the right loop to carry out a given operation can be difficult. The loop can be identified using the following information.
- Examine the issue and determine if a pre-test or post-test loop is required. The pre-test loop involves testing the condition prior to the loop’s entry. The condition is examined in the post-test loop subsequent to the loop’s entry.
- Choose the while or for loop if a pre-test loop is needed.
- Choose the do-while loop if we need a post-test loop.