Search⌘ K
AI Features

The break and continue Statements

Explore how Dart's break and continue statements manage loop behavior by exiting loops early or skipping iterations. Learn to apply these controls effectively, including their impact in nested loops, to write efficient and clear Dart code.

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 ...