Dart continue Statement Tutorial With Examples

0
6977
dart continue keyword
continue keyword in dart

continue keyword is used to break only one iteration of a loop when the defined condition is satisfied. In this tutorial, we will learn about Dart continue statement. You may already have used continue keyword in a switch case statement but continue keyword in loops have different functionality. continue keyword can also be used in a loop to skip the current execution of the loop block. Loop can be a for loop, while loop or do while loop. 

continue can also be very handy when we are writing complex loops. 

continue only break one iteration in loop and loop start from the next iteration.

Dart continue loop Examples

Consider a scenario where we want to print numbers from 1 to 10 expect number 5.

  
void main() {
  for (var i = 1; i <= 10; i++) {
    if (i == 5) {
      continue;
    }
    print(i);
  }
}

A continue keyword is used when the value of i is 5. The loop will miss rest of the code of this block and will start the next iteration. The number 5 is missing on the output console.

1
2
3
4
6
7
8
9
10

Dart continue for loop

As an example, We just want to print all the odd number from 1 to 10. Now continue keyword can help us. We will find the even number and then skip these values using continue keyword and at the end we will have all the odd numbers from 1 to 10.

 
void main() {
  for (var i = 1; i <= 10; i++) {
    if (i % 2 == 0) {
      continue;
    }
    print(i);
  }
}

The result is.

 
1
3
5
7
9

Dart continue While loop

This time execute the same code but using the while loop and a break statement. Pay special attention to the incremental variable.

 
void main() {
  var i = 1;
  while (i <= 10) {
    if (i % 2 == 0) {
      i++;
      continue;
    }
    print(i);
    i++;
  }
}

The values on the console are

 
1
3
5
7
9

Dart continue Do While loop

In a very similar way do while loop can also be broken for one iteration using a continue keyword. The same code is used but in a do while loop with a continue keyword.

 
void main() {
  var i = 1;
  do {
    if (i % 2 == 0) {
      i++;
      continue;
    }
    print(i);
    i++;
  } while (i <= 10);
}

The correct answer on the console screen is

 
1
3
5
7
9

Dart continue nested loop

If there is a nested loop means loop within a loop then continue will simply stop the inner loop execution once the condition is met but not of the outer loop unless loops are defined with labels. Let’s understand both of the concepts using example codes. 

We want to skip the execution of the rest of the code block when the value of i and j is equal to 2.

 
void main() {
  for (var i = 1; i <= 3; i++) {
    for (var j = 1; j <= 3; j++) {
      if (i == 2 && j == 2) continue;
      print('$i $j');
    }
  }
}

In the output 2,2 numbers are missing and that is expected answer.

 
1 1
1 2
1 3
2 1
2 3
3 1
3 2
3 3

As you may have noticed here continue keyword is only working on inner loop and there is no effect on the outer loop. The whole nested loop can be broken with a continue keyword if we label our loops. Let’s label the parent loop as outLoop and child loop and inLoop. These are variable names you can rename if you like. Now while writing the continue keyword in Dart simply give the loop a label that you want to break. In this case, we want to break the outLoop so we will put the label outLoop next to continue keyword.

 
void main() {
  outLoop:for (var i = 1; i <= 3; i++) {
    inLoop:for (var j = 1; j <= 3; j++) {
      if (i == 2 && j == 2) continue outLoop;
      print('$i $j');
    }
  }
}

After running the code there is expected result. Breaking the complete parent loop once the continue condition is reached with outLoop label.

 
1 1
1 2
1 3
2 1
3 1
3 2
3 3

This is pretty much all about Dart continue statement. Please let me know about your thoughts.

LEAVE A REPLY

Please enter your comment!
Please enter your name here