In programming languages, loops are used to execute a set of instructions/functions repeatedly when some conditions become true. There are three types of loops in Dart.

There are 3 types of loops present in dart, they are –

  • while loop
  • for loop
  • do-while loop

Let’s discuss one by one below.

For loop

The ‘for’ statement provides a compact way to iterate over a range of values. Programmers often refer to it as the “for loop” because of the way in which it repeatedly loops until a particular condition is satisfied. The general form of the ‘for’ statement can be expressed as follows:

Syntax –

for (initialization; termination;
increment) {
statement(s)
}

When using this version of the ‘for’ statement, keep in mind that:

  • The initialization expression initializes the loop; it’s executed once, as the loop begins.
  • When the termination expression evaluates to false, the loop terminates.
  • The increment expression is invoked after each iteration through the loop; it is perfectly acceptable for this expression to increment or decrement a value.

Let’s make a code, which will print “welcome to microcodes.in” 5 times using for loop.

Code 1:

void main(){
  for (int i = 0; i < 5; i++) {
    print(‘welcome to microcodes.in’);
  }
}
output

While loop

The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as:

while (expression) {
statement(s)
}

The while statement evaluates expression, which must return a Boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false.

Let’s make a code which will print “this is a while loop” 4 times using while loop.

Code 2:

void main(){
  var i=0;
  while (i<4) {
    print(‘this is a while loop’);
    i++;
  }
}
output

do-while loop

The Dart programming language also provides a do-while statement, which can be expressed as follows: Syntax –

do {
statement(s)
} while (expression);  

The difference between do-while and while is that do-while evaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within the do block are always executed at least once. Let’s understand this below code…

Code 3:

void main(){
  var i=0;
  do {
    print(‘Sir Gurudas Mahavidyalaya’);
    i++;
  } while (i<3);
}
output

break statement

When we working on loops in dart, we sometime needs to terminate that loop instantly.

In that situation, we can use ‘break’ keyword. Let’s understand this with below code…

Code 4:

void main(){
  int b=0;
  while (b<4) {
    print(‘this is a while loop!’);
    break;
    b++;
  }
}
output

code 5:

void main(){
 for(int i=0;i<3;i++){
  print(‘this is a for loop!’);
  break;
 }
}
output

Due to ‘break’ statement uses it terminates the loop instantly.

Continue statement

The continue statement skips the current iteration of a for, while, or do-while loop. The unlabeled form skips to the end of the innermost loop’s body and evaluates the Boolean expression that controls the loop.

Let’s make a code, which will inputs total 5 numbers (odd/even) and it print the addition for only even numbers.

Code 6:

import ‘dart:io’;
 
void main(){
  String? str;
  late int a;
  int sum=0;
 
  for (var i = 0; i < 5; i++) {
    print(‘Enter Digit: ‘);
    str=stdin.readLineSync();
    if (str!=null) {
      a=int.parse(str);   
    }
    if((a%2)==1){
      // we skips addition part for odd numbers
      continue;
    }
    else{
        sum=sum+a;
    }
  }
 
  print(‘even number addition is ‘);
  print(‘$sum’);
 
}
output

Let’s make a code, which will accepts only 5 strings which first character start with ‘a’ and print all strings summation of them.

Code 7:

import ‘dart:io’;
 
void main(){
  String? name;
 String sum=””;
  print(‘Enter String starting with a or A: ‘);
  for (var i = 0; i < 5; i++) {
    print(‘Enter String: ‘);
    name=stdin.readLineSync();
    if (name!=null) {
       if (name[0]==’a’ || name[0]==’A’) {
        sum=sum+name;    
    }
    }
}
print(‘String summation: ‘);
print(sum);
}
output

Nested loops

A loop inside another loop is called a nested loop. The depth of nested loop depends on the complexity of a problem.

Syntax –

while/do-while/for loop // outer loop{
while/do-while/for loop // inner loop
{
statements
}
}

Let’s understand simple concepts of inner loop and outer loop with some examples below.

Nested loop (while loop)

Code 8:

void main(){
  int i=0,j=0;
 
  // outer loop
  while (i<3) {
    print(‘outer loop’);
    // inner loop
    while (j<2) {
      print(‘inner loop’);
      j++;
    }
    j=0;
    i++;
  }
}
output

Nested ‘for loop’

Code 9:

void main(){
 
  // outer loop
  for (var i = 0; i < 3; i++) {
    print(‘outer for loop’);
    // inner loop
    for (var j = 0; j < 2; j++) {
      print(‘inner for loop’);
    }
  }
}
output

Nested ‘do-while loop’

Code 10

void main(){
  int i=0,j=0;
 
  // outer loop
  do {
    print(‘outer do-while loop’);
 
    // inner loop
    do {
      print(‘inner do-while loop’);
      j++;
    } while (j<2);
    j=0;
    i++;
  } while (i<3);
}
output