Search⌘ K
AI Features

The break and continue Statements

Explore how break and continue statements control the flow of loops in Dart. Learn to use break to exit loops early and continue to skip specific iterations while maintaining loop execution. Understand their scope within nested loops to write more efficient and clear loop logic.

We sometimes encounter situations where we need to alter the standard flow of a loop based on a specific condition. We might want to stop the loop entirely before it finishes all its iterations. Alternatively, we might want to skip the current iteration and move directly to the next one.

Dart provides the break and continue statements to handle these exact scenarios. The break statement exits the loop completely. The continue statement skips the remainder of the current iteration. Both statements apply exclusively to the nearest enclosing loop.

The break statement

We use the break keyword to prematurely terminate a loop. When the program encounters a break statement, it exits the loop immediately, regardless of whether the loop has completed its planned iterations.

Suppose we have a list of integers and we only want to find the very first even number. We can use a break statement to ...