Summary: in this tutorial, you’ll learn how to use the Dart break
statement to terminate a loop prematurely.
Introduction to the Dart break statement
The break
statement prematurely terminates a loop including while
, do while
, and for
loops. The following shows the syntax of the break
statement:
break;
Code language: Dart (dart)
In practice, you’ll use the break
statement with an if
statement to terminate a loop based on a condition. For example, the following checks a condition and only terminates the loop if the condition is true
:
while (expression)
{
if (condition)
{
break;
}
}
Code language: Dart (dart)
Note that you have statements before and/or after the if
statement. The following flowchart shows how the break
statement works:
Similarly, you can use the break
statement inside a do while
loop as follows:
do
{
if (condition)
{
break;
}
} while (expression);
Code language: Dart (dart)
And the following flowchart illustrates how the break
statement works inside a do while
loop:
The following shows how to use the break
statement inside a for
loop:
for (initializer; condition; iterator)
{
if (condition)
{
break;
}
}
Code language: Dart (dart)
This flowchart illustrates how the break
statement works in the for
loop:
When you use the break
statement inside a nested loop, it only terminates the enclosing loop, not all the loops.
Dart break statement examples
Let’s take some examples of using the Dart break
statement.
1) Using Dart break statement inside a for loop examples
The following example uses the break
statement to terminate a for
loop once it found the first occurrence of the letter s
in a string:
void main() {
String message = "Dart is awesome!";
for (int i = 0; i < message.length; i++) {
if (message[i] == 's') {
print("The letter s is found at index $i");
break;
}
}
}
Code language: Dart (dart)
Output:
The letter s is found at index 6
Code language: plaintext (plaintext)
This example uses a for
loop to iterate over the letters of the string “Dart is awesome”. It compares the current letter with the letter “s” in each iteration. And it terminates the loop immediately if the current letter is the letter “s”.
Without the break
statement, the for loop will iterate over all characters in the string message even though it already found the letter s.
If the message
string doesn’t have the letter s
, the condition in the if
statement will evaluate to false
and the break
statement will never be reached. As the result, the for
loop will iterate over all characters in the message
string.
2) Using the Dart break statement inside a while loop example
The following example uses a while
loop to find the first occurrence of the letter a
in a string:
void main() {
String message = 'Dart is awesome!';
int i = 0;
while (i < message.length) {
print(message[i]);
if (message[i] == 'a') {
print("The letter a was found at the index $i");
break;
}
i++;
}
}
Code language: Dart (dart)
3) Using the Dart break statement inside a do while loop
The following example uses the break
statement inside a do while
statement to terminate the loop if it finds the letter 'a'
in a string:
void main() {
String message = 'Dart is awesome!';
int i = 0;
do {
print(message[i]);
if (message[i] == 'a') {
print("The letter a was found at the index $i");
break;
}
i++;
} while (i < message.length);
}
Code language: Dart (dart)
Output:
D
a
The letter a was found at the index 1
Code language: plaintext (plaintext)
Summary
- Use the
break
statement to prematurely terminate a loop includingwhile
,do while
, andfor
. - The
break
statement terminates only the enclosing loop when you use it in a nested loop.