Dart break Statement Tutorial With Examples

0
4599
dart break keyword
break keyword in dart

break keyword is used to break the flow of code. In this tutorial, we will learn about Dart break statement. You may already have used break keyword in a switch case statement. break keyword can also be used in a loop to stop the execution of the loop block. Loop can be a for loop, while loop or do while loop. 

break: As soon as execution reaches the break keyword in a loop it breaks the loop. Loop execution stops no matter what if the condition still holds true.

Dart break loop Examples

As an example, We just want to print the first even number from 1 to 10. Now break can help us. As we get the first even number we will simply use a break keyword.

Dart break for loop

  
void main() {
  for (var i = 1; i <= 10; i++) {
    if (i % 2 == 0) {
      print(i);
      break;
    }
  }
}
//The output is 2.

Dart break While loop

This time execute the same code but using the while loop and a break statement.

  
void main() {
  var i = 1;
  while (i <= 10) {
    if (i % 2 == 0) {
      print(i);
      break;
    }
    i++;
  }
}
//The output is 2.

Dart break Do While loop

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

  
void main() {
  var i = 1;
  do {
    if (i % 2 == 0) {
      print(i);
      break;
    }
    i++;
  } while (i <= 10);
}
//The output is 2.

Dart break nested loop

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

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

For each value of i the inner j loop code is executed 3 times. The result looks like this. 

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

Now we have to keep the same code but execute the inner j loop only 2 times not 3 times. 

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

We are breaking the inner loop when the value of j is equal to 3. After the code run, the console shows the following results.

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

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

  
void main() {
  outerLoop:for (var i = 1; i <= 3; i++) {
   innerLoop: for (var j = 1; j <= 3; j++) {
      if (j == 3) break outerLoop;
      print('$i $j');
    }
  }
}

After running the code there is expected result.

  
1 1
1 2

This is pretty much all about Dart break statement. Hope the topic was clear enough. Please let us know what do you guys think.

LEAVE A REPLY

Please enter your comment!
Please enter your name here