Break and Continue

When working with loops in Dart, sometimes you need more control over the flow of execution. That's where break and continue statements come in handy! Think of them as traffic signals for your loops.

Break Statement

The break statement is like an emergency exit for your loop. When encountered, it immediately terminates the loop and jumps to the code after the loop.

Syntax

break;

Example with For Loop

void main() {
  print("Counting until we hit 5:");
  
  for (int i = 1; i <= 10; i++) {
    if (i == 5) {
      print("Found 5! Breaking out of loop.");
      break;
    }
    print("Number: $i");
  }
  
  print("Loop ended!");
}

Output:

Counting until we hit 5:
Number: 1
Number: 2
Number: 3
Number: 4
Found 5! Breaking out of loop.
Loop ended!

Example with While Loop

void main() {
  int attempts = 0;
  
  while (true) {
    attempts++;
    print("Attempt $attempts");
    
    if (attempts == 3) {
      print("Maximum attempts reached!");
      break;
    }
  }
  
  print("Done trying!");
}

Continue Statement

The continue statement is like skipping a song on your playlist. It skips the rest of the current iteration and jumps to the next iteration of the loop.

Syntax

continue;

Example with For Loop

void main() {
  print("Printing only even numbers:");
  
  for (int i = 1; i <= 10; i++) {
    if (i % 2 != 0) {
      continue; // Skip odd numbers
    }
    print("Even number: $i");
  }
}

Output:

Printing only even numbers:
Even number: 2
Even number: 4
Even number: 6
Even number: 8
Even number: 10

Example with While Loop

void main() {
  int i = 0;
  
  while (i < 5) {
    i++;
    
    if (i == 3) {
      print("Skipping number 3");
      continue;
    }
    
    print("Processing number: $i");
  }
}

Practical Example: Input Validation

Here's a real-world example combining both break and continue:

void main() {
  List<String> userInputs = ["5", "abc", "10", "xyz", "15", "quit"];
  
  print("Processing user inputs:");
  
  for (String input in userInputs) {
    // Break if user wants to quit
    if (input == "quit") {
      print("User requested to quit. Goodbye!");
      break;
    }
    
    // Try to parse as number
    int? number = int.tryParse(input);
    
    // Continue if input is not a valid number
    if (number == null) {
      print("'$input' is not a valid number. Skipping...");
      continue;
    }
    
    // Process valid number
    print("Processing number: $number (doubled: ${number * 2})");
  }
}

Key Points to Remember

StatementPurposeEffect
breakExit loop completelyJumps to code after the loop
continueSkip current iterationJumps to next iteration

When to Use Break and Continue

  • Use break when:

    • You've found what you're looking for
    • An error condition occurs
    • You want to exit based on user input
  • Use continue when:

    • You want to skip invalid data
    • Certain conditions don't need processing
    • You want to filter items in a loop

These control statements make your loops more flexible and help you write cleaner, more efficient code! 🚀