Dart - Loops

At times, certain instructions 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 termed as an iteration.

The following figure illustrates the classification of loops −

Classification Of Loops

Let’s start the discussion with Definite Loops. A loop whose number of iterations are definite/fixed is termed as a definite loop.

[1]. for loop

The for loop is an implementation of a definite loop. The for loop executes the code block for a specified number of times. It can be used to iterate over a fixed set of values, such as an array

Following is the syntax of the for loop.

for (initial_count_value; termination-condition; step) { 
   //statements 
}    


Example

void main() { 
   var num = 5; 
   var factorial = 1; 
   
   for( var i = num ; i >= 1; i-- ) { 
      factorial *= i ; 
   } 
   print(factorial); 
}

The program code will produce the following output −

120


  • The for loop has three parts: the initializer (i=num), the condition ( i>=1) and the final expression (i--).
  • The program calculates the factorial of the number 5 and displays the same. The for loop generates the sequence of numbers from 5 to 1, calculating the product of the numbers in every iteration.
  • Multiple assignments and final expressions can be combined in a for loop, by using the comma operator (,). For example, the following for loop prints the first eight Fibonacci numbers −
Example

void main() { 
   for(var temp, i = 0, j = 1; j<30; temp = i, i = j, j = i + temp) { 
      print('${j}'); 
   } 
} 

It should produce the following output −

1 
1 
2 
3 
5
8 
13 
21


[2]. for…in Loop

The for...in loop is used to loop through an object's properties.

Following is the syntax of ‘for…in’ loop.

for (variablename in object){  
   statement or block to execute  
}

In each iteration, one property from the object is assigned to the variable name and this loop continues till all the properties of the object are exhausted.

Example

void main() { 
   var obj = [12,13,14]; 
   
   for (var prop in obj) { 
      print(prop); 
   } 
} 

It should produce the following output −

12 
13 


[3]. 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 block of code is executed.

Following is the syntax for the while loop.

while (expression) {
   Statement(s) to be executed if expression is true  
}


Example

void main() { 
   var num = 5; 
   var factorial = 1; 
   
   while(num >=1) { 
      factorial = factorial * num; 
      num--; 
   } 
   print("The factorial  is ${factorial}"); 
}  

The above code uses a while loop to calculate the factorial of the value in the variable num.

The following output is displayed on successful execution of the code.

The factorial is 120 


[4]. 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.

Following is the syntax for the do-while loop.

do {  
   Statement(s) to be executed;  
} while (expression); 

Note − Don’t miss the semicolon used at the end of the do...while loop.

Example

void main() { 
   var n = 10; 
   do { 
      print(n); 
      n--; 
   }
   while(n>=0); 
}  

The example prints numbers from 0 to 10 in the reverse order. The following output is displayed on successful execution of the above code.

10 
9 
8 
7 
6 
5 
4 
3 
2 
1 
0

[5]. Break Statement

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.

Example

void main() { 
   var i = 1; 
   while(i<=10) { 
      if (i % 5 == 0) { 
         print("The first multiple of 5  between 1 and 10 is : ${i}"); 
         break ;    
         //exit the loop if the first multiple is found 
      } 
      i++; 
   }
}  

The above code prints the first multiple of 5 for the range of numbers within 1 to 10.

If a number is found to be divisible by 5, the if construct forces the control to exit the loop using the break statement. The following output is displayed on successful execution of the above code.

The first multiple of 5 between 1 and 10 is: 5 


[6]. continue Statement

The continue statement skips the subsequent statements in the current iteration and takes the control back to the beginning of the loop. Unlike the break statement, the continue statement doesn’t exit the loop. It terminates the current iteration and starts the subsequent iteration.

The following example shows how you can use the continue statement in Dart −

Example

void main() { 
   var num = 0; 
   var count = 0; 
   
   for(num = 0;num<=20;num++) { 
      if (num % 2==0) { 
         continue; 
      } 
      count++; 
   } 
   print(" The count of odd values between 0 and 20 is: ${count}"); 
}  

The above example displays the number of even values between 0 and 20. The loop exits the current iteration if the number is even. This is achieved using the continue statement.

The following output is displayed on successful execution of the above code.

The count of odd values between 0 and 20 is: 10 
[7]. Using Labels to Control the Flow

label is simply an identifier followed by a colon (:) that is applied to a statement or a block of code. A label can be used with break and continue to control the flow more precisely.

Line breaks are not allowed 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.

Example: Label with Break


void main() { 
   outerloop: // This is the label name 
   
   for (var i = 0; i < 5; i++) { 
      print("Innerloop: ${i}"); 
      innerloop: 
      
      for (var j = 0; j < 5; j++) { 
         if (j > 3 ) break ; 
         
         // Quit the innermost loop 
         if (i == 2) break innerloop; 
         
         // Do the same thing 
         if (i == 4) break outerloop; 
         
         // Quit the outer loop 
         print("Innerloop: ${j}"); 
      } 
   } 
}

The following output is displayed on successful execution of the above code.

Innerloop: 0
Innerloop: 0
Innerloop: 1
Innerloop: 2
Innerloop: 3
Innerloop: 1
Innerloop: 0
Innerloop: 1
Innerloop: 2
Innerloop: 3
Innerloop: 2
Innerloop: 3
Innerloop: 0
Innerloop: 1
Innerloop: 2
Innerloop: 3
Innerloop: 4

Example: Label with continue


void main() { 
   outerloop: // This is the label name 
   
   for (var i = 0; i < 3; i++) { 
      print("Outerloop:${i}"); 
      
      for (var j = 0; j < 5; j++) { 
         if (j == 3){ 
            continue outerloop; 
         } 
         print("Innerloop:${j}"); 
      } 
   } 
}

The following output is displayed on successful execution of the above code.

Outerloop: 0 
Innerloop: 0 
Innerloop: 1 
Innerloop: 2 

Outerloop: 1 
Innerloop: 0 
Innerloop: 1 
Innerloop: 2 

Outerloop: 2 
Innerloop: 0 
Innerloop: 1 
Innerloop: 2 




Comments

Popular posts from this blog

Java : Variables Declaring

Install DNF in RHEL/CentOS 7

SQL Self JOIN