Break and Continue Statements in Dart
In Dart, break
and continue
are control flow statements that allow you to alter the normal flow of loops. They provide mechanisms to manage how loops behave under certain conditions. Understanding how to use these statements can lead to more efficient and readable code.
Break Statement
Overview
The break
statement is used to exit a loop prematurely, terminating the loop's execution regardless of whether the loop's condition is still true. It can be particularly useful when you want to stop looping based on a specific condition.
Syntax
break;
Example of Break Statement
Here’s an example that demonstrates the use of the break
statement within a for
loop to find the first number greater than a certain value.
Code:
dart
Copy
void main() {
for (int i = 1; i <= 10; i++) {
if (i > 5) {
print('First number greater than 5 is: $i');
break; // Exit the loop when i is greater than 5 }
}
}
Explanation:
- The loop iterates from 1 to 10.
- The
if
statement checks ifi
is greater than 5. - When
i
becomes 6, it prints the message and executesbreak
, terminating the loop immediately.
Continue Statement
Overview
The continue
statement is used to skip the current iteration of a loop and continue with the next iteration. This is useful when you want to bypass certain conditions within the loop without terminating the loop entirely.
Syntax
continue;
Example of Continue Statement
Here’s an example that illustrates the use of the continue
statement to skip even numbers in a for
loop.
Code:
void main() {
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue; // Skip the rest of the loop for even numbers }
print(i); // Print only odd numbers }
}
Explanation:
- The loop iterates from 1 to 10.
- The
if
statement checks ifi
is even using the modulus operator. - If
i
is even, thecontinue
statement is executed, which skips theprint(i);
statement for that iteration. - As a result, only odd numbers are printed.
Combined Example: Break and Continue
This example demonstrates both break
and continue
statements within a single loop.
Code:
void main() {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
print('Breaking the loop at i = $i');
break; // Exit the loop when i equals 5 }
if (i % 2 == 0) {
continue; // Skip even numbers }
print(i); // Print only odd numbers }
}
Explanation:
- The loop iterates from 1 to 10.
- If
i
equals 5, it prints a message and exits the loop usingbreak
. - If
i
is even, it skips the current iteration usingcontinue
. - Thus, the output will show odd numbers up to 3, and then it stops when
i
reaches 5.
Conclusion
The break
and continue
statements in Dart are powerful tools for controlling loop execution. The break
statement allows you to exit a loop immediately when a certain condition is met, while the continue
statement lets you skip the current iteration and proceed to the next one. Understanding how to use these statements effectively can help you write clearer and more efficient code in your Dart applications.
PLAY QUIZ