On this page11 sections
  1. Prerequisites
  2. Learning Objectives
  3. 1. break: Stop the Loop
  4. 2. Searching With break
  5. 3. continue: Skip to the Next Round
  6. 4. while (true) With break
  7. Code Examples
  8. Common Beginner Mistakes
  9. Practice Questions
  10. Practice Answers
  11. Key Takeaways

Lecture 023: break & continue

Day9: break, continue & Labels
Lecture023 of 057
Estimated video length12 min
TargetDart 3.12
LevelBeginner

Searching a list for a name? Once you've found it, there's no point checking the rest. Adding up marks but some entries are invalid? Skip those and carry on. break stops a loop early; continue skips to the next round.

Contents

Prerequisites · Objectives · Lesson · Code Examples · Common Mistakes · Practice Questions · Answers · Takeaways


Prerequisites

Lectures 020–022 (loops).


Learning Objectives

  • Leave a loop early with break
  • Skip the rest of one round with continue
  • Search a list and stop at the first match
  • Use while (true) with break safely

1. break: Stop the Loop

break ends the loop immediately. The program carries on after the loop.

dart
for (var i = 1; i <= 10; i++) {
  if (i == 5) {
    break;
  }
  print(i);
}
print('Loop finished');

Output

code
1
2
3
4
Loop finished

When i reaches 5, the loop stops. 5 to 10 are never printed.

2. Searching With break

dart
var names = ['Ali', 'Sara', 'Hamza', 'Zara'];
var target = 'Hamza';
var foundAt = -1;
for (var i = 0; i < names.length; i++) {
  if (names[i] == target) {
    foundAt = i;
    break;   // found it, no need to check Zara
  }
}
print('$target found at index $foundAt');
// Hamza found at index 2

foundAt starts at -1, meaning "not found", in case the name isn't in the list.

3. continue: Skip to the Next Round

continue skips the rest of the current round and moves straight on to the next one:

dart
for (var i = 1; i <= 6; i++) {
  if (i % 2 == 0) {
    continue;
  }
  print('odd: $i');
}

Output

code
odd: 1
odd: 3
odd: 5
dart
var marks = [80, -1, 65, -1, 90];   // -1 means "absent"
var total = 0;
for (var m in marks) {
  if (m < 0) continue;
  total += m;
}
print('Total of valid marks: $total');   // 235
KeywordEffect
breakleave the whole loop now
continueskip the rest of this round, go to the next

4. while (true) With break

Sometimes the stopping point is easiest to check in the middle of the loop. Write while (true) and break when you're done:

dart
var n = 0;
while (true) {
  n++;
  if (n > 3) break;
  print('while true: $n');
}

Output

code
while true: 1
while true: 2
while true: 3

Make sure the break is always reachable, or you've written an infinite loop (Lecture 021).


Code Examples

Example 1: Processing Orders

dart
void main() {
  var orders = [450, 0, 1200, -50, 800, 99999, 300];
  var processed = 0;
  var revenue = 0;

  for (var amount in orders) {
    if (amount == 99999) {
      print('Stop signal received');
      break;
    }
    if (amount <= 0) {
      print('Skipping invalid order: $amount');
      continue;
    }
    revenue += amount;
    processed++;
    print('Processed Rs $amount');
  }
  print('Orders processed: $processed, revenue: Rs $revenue');
}

Output

code
Processed Rs 450
Skipping invalid order: 0
Processed Rs 1200
Skipping invalid order: -50
Processed Rs 800
Stop signal received
Orders processed: 3, revenue: Rs 2450

Invalid orders are skipped with continue. The special value 99999 stops everything with break, so the Rs 300 order after it is never processed.


Common Beginner Mistakes

1. continue before the counter update in a while loop

dart
var i = 0;
while (i < 5) {
  if (i == 2) {
    continue;
  }
  print(i);
  i++;
}

This prints 0, 1 and then loops forever: when i is 2, continue jumps back before i++ can run, so i stays 2. Update the counter before continue, or use a for loop, where the step always runs.

2. break outside a loop

dart
if (x > 3) {
  break;
}
code
Error: A break statement can't be used outside of a loop or switch statement.

3. Expecting break to leave both loops: in nested loops, break only leaves the innermost loop. Lecture 024 shows how to leave the outer one.

4. Mixing them up: break = done with the loop; continue = done with this item only.


Practice Questions

  1. What does this print? for (var i = 1; i <= 5; i++) { if (i == 3) break; print(i); }
  2. And with continue instead of break?
  3. Write a loop over [4, 7, 0, 9] that stops at the first 0.
  4. Write a loop that prints only the names longer than 3 letters, using continue.
  5. In nested loops, which loop does break leave?

Practice Answers

  1. 1, 2
  2. 1, 2, 4, 5
dart
for (var n in [4, 7, 0, 9]) {
  if (n == 0) break;
  print(n);
}
dart
for (var name in names) {
  if (name.length <= 3) continue;
  print(name);
}
  1. Only the innermost loop that contains it.

Key Takeaways

  • break leaves the loop immediately; continue skips to the next round.
  • Use break to stop a search at the first match.
  • Use continue to skip invalid items.
  • while (true) + break works if the break is always reachable.
  • In a while loop, update the counter before any continue.