Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The break statement is an unconditional control flow construct used to prematurely terminate the execution of the innermost enclosing loop (for, while, do-while) or switch statement. Upon execution, control is immediately transferred to the statement strictly following the terminated control structure, bypassing any remaining iterations or case blocks.

Syntax

// Unlabeled break
break;

// Labeled break
break label_name;

Mechanics

Dart supports two variations of the break statement: unlabeled and labeled.

Unlabeled Break

When an unlabeled break is encountered, the Dart runtime halts the current iteration of the immediate enclosing loop and prevents any subsequent iterations. The loop’s condition is not evaluated again.
for (int i = 0; i < 5; i++) {
  if (i == 3) {
    break; // Immediately terminates the 'for' loop
  }
  print(i); 
}
// Control transfers here after the break
In a switch statement, break prevents fall-through by terminating the switch block after a matching case is executed. (Note: In Dart 3.0 and later, break is no longer strictly required at the end of non-empty case clauses, but it remains valid syntax).
int value = 1;

switch (value) {
  case 1:
    print('One');
    break; // Terminates the switch statement
  case 2:
    print('Two');
}

Labeled Break

Dart allows loops to be prefixed with an identifier followed by a colon, creating a label. A labeled break statement explicitly specifies which enclosing loop to terminate. This is strictly utilized within nested loop architectures to bypass the default “innermost-only” termination rule, allowing a deeply nested block to terminate an outer loop.
outerLoop: // Label declaration
for (int i = 0; i < 3; i++) {
  for (int j = 0; j < 3; j++) {
    if (i == 1 && j == 1) {
      break outerLoop; // Terminates the loop identified by 'outerLoop'
    }
    print('i: $i, j: $j');
  }
}
// Control transfers here after 'break outerLoop'

Execution Rules

  • A break statement must be lexically enclosed by a loop or switch statement.
  • A labeled break must reference a valid, enclosing label within the same function scope. It cannot be used to transfer control to arbitrary labels outside its lexical hierarchy (it is not a goto statement).
  • Executing a break does not execute the loop’s update statement (e.g., the i++ in a for loop) for the terminated iteration.
Master Dart with Deep Grasping Methodology!Learn More