loading

Dart for Loop

When we are familiar with how many times a block of code has been executed, we employ the dart for loop. It is comparable to the for loop in Java, C, and C++. For the loop to begin, an initial variable is required. It runs a block of code until the given condition is met. Every time the loop is run, the value of the iterator is changed, and the test expression is then assessed. This method will keep going until the test-expression that is provided is true. The for loop ends if the test-expression returns false.

Dart for Loop Flowchart

Dart For Loop -

Syntax

				
					for(initialization, condition, incr/decr)  
				
			
  • The initialization only runs once and is used as the beginning value in a loop.
  • Boolean values, True or False, are returned by a condition or test expression. Until the condition is met, the for loop will keep running.
  • The loop ends when the condition evaluates false.
  • The variable can be increased or decreased using the incr/decr counter.

Example

				
					void main() {  
    //for loop iteration   
    for(int i = 1; i < =10;i++)  
    {  
        print(i);  
}  
}  
				
			

Output

				
					1
2
3
4
5
6
7
8
9
10
				
			

Explanation

We have initialized an integer variable, i, with the initial value in the example above. The variable now has a value of 1, and in the conditional section, we have constructed a loop that will run until the value of i is either smaller than or equal to 10. The value of the loop will rise by one every time iteration.

The value of i is increased by 1 in the first iteration of a loop, becoming 2. The loop will now advance to the following iteration if the condition is verified once again. The loop will continue to iterate until the value reaches 10.

The for loop’s starting value can be skipped. Think about the example that follows.

Example

				
					void main() {  
     var i = 1;  
    //for loop iteration skipping the initial value from for loop  
    for(; i < =10;i++)  
    {  
        print(i);  
}  
}  
				
			

The output will be the same as with the earlier code.

Additionally, by use a semicolon, we can omit the condition, increment, or decrement.

Nested for Loop

“The for loop inside another for loop” is the definition of a nested for loop. An exterior loop is referred to as an outer loop, and an inner loop is referred to as one inside another loop. The inner loop will repeat itself through to the end of its cycle in each iteration of the outer loop. Let’s examine the nested for loop sample that follows.

Example

				
					void main()   
{  
int i, j;  
int table_no = 2;  
int max_no = 10;  
for (i = 1; i <= table_no; i++) { // outer loop  
  for (j = 0; j <= max_no; j++) { // inner loop  
    print("${i} * ${j} = ${i*j}");  
  //print("\n"); /* blank line between tables */  
}}  
  
}
				
			

Output

				
					1 * 0 = 0
1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
1 * 4 = 4
1 * 5 = 5
2 * 0 = 0
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
				
			

Let’s see how a nested for loop operates.

Example

				
					void main(){  
for(int i = 1; i <=5; i++) {  
         
    print("Outer loop iteration : ${i}" );  
  
         for (int j = 1; j <= i; ++j) {  
            print("i = ${i} j = ${j}");  
         }  
           
      }  
   }  
				
			

Output

				
					Outer loop iteration : 1
i = 1 j = 1
Outer loop iteration : 2
i = 2 j = 1
i = 2 j = 2
Outer loop iteration : 3
i = 3 j = 1
i = 3 j = 2
i = 3 j = 3
Outer loop iteration : 4
i = 4 j = 1
i = 4 j = 2
i = 4 j = 3
i = 4 j = 4
Outer loop iteration: 5
i = 5 j = 1
i = 5 j = 2
i = 5 j = 3
i = 5 j = 4
i = 5 j = 5
				
			

Take a look at the code above; it defines how the inner loop operates. For every iteration of the outer loop, the inner loop will be repeated.

Share this Doc

Dart for Loop

Or copy link

Explore Topic