Dart supports two types of loop control statements:
- Break Statement
- Continue Statement
Break Statement:
This statement is used to break the flow of control of the loop i.e if it is used within a loop then it will terminate the loop whenever encountered. It will bring the flow of control out of the nearest loop.
Syntax:
break;
Example 1: Using break inside while loop
void main() { int count = 1; while (count <= 10) { print("Geek, you are inside loop $count"); count++; if (count == 4) { break; } } print("Geek, you are out of while loop"); } |
Output:
Geek, you are inside loop 1 Geek, you are inside loop 2 Geek, you are inside loop 3 Geek, you are out of while loop
Explanation:
Initially count value is 1, as it goes inside loop the condition is checked, 1 <= 10 and as it is true the statement is printed variable is increased and then condition is checked, 2 == 4, which is false. Then the loop is followed again till the condition 4 == 4 is encountered and the flow comes out of the loop and then last print statement is executed.
Example 2: Using break inside do..while loop
void main() { int count = 1; do { print("Geek, you are inside loop $count"); count++; if (count == 5) { break; } } while (count <= 10); print("Geek, you are out of do..while loop"); } |
Output:
Geek, you are inside loop 1 Geek, you are inside loop 2 Geek, you are inside loop 3 Geek, you are inside loop 4 Geek, you are out of do..while loop
Example 3: Using break inside for loop
void main() { for (int i = 1; i <= 10; ++i) { if (i == 2) break; print("Geek, you are inside loop $i"); } print("Geek, you are out of loop"); } |
Output:
Geek, you are inside loop 1 Geek, you are out of loop
Continue Statement:
While the break is used to end the flow of control, continue on the other hand is used to continue the flow of control. When a continue statement is encountered in a loop it doesn’t terminate the loop but rather jump the flow to next iteration.
Syntax:
continue;
Example 1: Using continue inside while loop
void main() { int count = 0; while (count <= 10) { count++; if (count == 4) { print("Number 4 is skipped"); continue; } print("Geek, you are inside loop $count"); } print("Geek, you are out of while loop"); } |
Output:
Geek, you are inside loop 1 Geek, you are inside loop 2 Geek, you are inside loop 3 Number 4 is skipped Geek, you are inside loop 5 Geek, you are inside loop 6 Geek, you are inside loop 7 Geek, you are inside loop 8 Geek, you are inside loop 9 Geek, you are inside loop 10 Geek, you are inside loop 11 Geek, you are out of while loop
Explanation:
Here control flow of the loop will go smooth but when count value becomes 4 the if condition becomes true and the below statement is skipped because of continue and next iteration skipping number 4.
Example 2: Using continue inside do..while loop
void main() { int count = 0; do { count++; if (count == 4) { print("Number 4 is skipped"); continue; } print("Geek, you are inside loop $count"); } while (count <= 10); print("Geek, you are out of while loop"); } |
Output:
Geek, you are inside loop 1 Geek, you are inside loop 2 Geek, you are inside loop 3 Number 4 is skipped Geek, you are inside loop 5 Geek, you are inside loop 6 Geek, you are inside loop 7 Geek, you are inside loop 8 Geek, you are inside loop 9 Geek, you are inside loop 10 Geek, you are inside loop 11 Geek, you are out of while loop
Example 3: Using continue inside for loop
void main() { for (int i = 1; i <= 10; ++i) { if (i == 2) { print("Geek, you are inside loop $i"); continue; } } print("Geek, you are out of loop"); } |
Output:
Geek, you are inside loop 2 Geek, you are out of loop
Recommended Posts:
- Assert Statements in Dart
- Dart - Break Statement
- Dart - Super and This keyword
- Dart - Classes And Objects
- Instance and class methods in Dart
- Getter and Setter Methods in Dart
- Dart - Finding Minimum and Maximum Value in a List
- Dart - Const And Final Keyword
- HTML Document Object Model and Dart Programming
- Variables and Keywords in Dart
- What is Lexical scope and lexical closures in Dart
- Dart - Getters and Setters
- Difference Between DART and C++
- Comparison of Dart and JavaScript
- Dart - Dates and Times
- Upload and Retrieve Images on MongoDB using Dart in Flutter
- Difference Between Golang and Dart
- How to Combine Lists in Dart?
- Basics of Numbers in Dart
- Dart - Standard Input Output
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai

