Break and Continue Statement in Dart

In Dart, the break and continue statements are control flow constructs used within loops to alter their normal execution flow. Let’s break down each one:


Break: The break statement is used to terminate the nearest enclosing loop. When encountered within a loop, it immediately exits the loop, allowing the program to continue executing the code following the loop.

Continue: On the other hand, the continue statement skips the rest of the current iteration of the loop and jumps to the next iteration. It effectively bypasses any code that follows it within the loop’s body for the current iteration.

In the below PDF we discuss about Break and Continue Statement in detail in simple language, Hope this will help in better understanding.

Dart Programming

Break Statement:

The break statement is used to terminate the nearest enclosing loop. When encountered within a loop, it immediately exits the loop, allowing the program to continue executing the code following the loop.

Break Statement Example:

void main() {
for (var i = 1; i <= 5; i++) {
print('Current number is $i');
if (i == 3) {
break; // Exit the loop when i equals 3
}
}
print('Loop ended');
}

Output:


Current number is 1
Current number is 2
Current number is 3
Loop ended

In this example, the loop terminates prematurely when i equals 3 due to the break statement, and the message “Loop ended” is printed.

Continue Statement:

the continue statement skips the rest of the current iteration of the loop and jumps to the next iteration. It effectively bypasses any code that follows it within the loop’s body for the current iteration.

Continue Statement Example:

void main() {
for (var i = 1; i <= 5; i++) {
if (i == 3) {
continue; // Skip iteration when i equals 3
}
print('Current number is $i');
}
}

Output:

Current number is 1
Current number is 2
Current number is 4
Current number is 5

Here, when i equals 3, the continue statement causes the loop to skip printing “Current number is 3” and proceeds to the next iteration.

Conclusion:

In conclusion, the break and continue statements in Dart provide developers with precise control over loop execution. Whether you need to prematurely exit a loop or skip certain iterations based on specific conditions, these statements empower you to fine-tune the behavior of your code efficiently.

Related Question

The break statement in Dart is used to exit a loop prematurely. When encountered, it immediately terminates the nearest enclosing loop, whether it’s a for, while, or do-while loop.

You place the break keyword within the loop body where you want the loop to exit. Upon encountering break, Dart will jump to the statement immediately following the loop.

No, break is specifically designed to be used within loops. Attempting to use break outside of a loop results in a compilation error.

The continue statement in Dart skips the remaining code inside a loop’s body for the current iteration and moves on to the next iteration of the loop.

While both break and continue alter the flow of control within loops, break exits the loop entirely, while continue skips the rest of the current iteration and moves to the next iteration of the loop.

Relevant

OOPS in Dart Object-Oriented Programming

Queue in Dart Programming A

Maps in Dart A Map

Set in Dart A Set

Dart Lists A Dart list

Strings in Dart A Dart

Functions in Dart In Dart,

Leave a Comment

Your email address will not be published. Required fields are marked *

// Sticky ads
Your Poster