On this page10 sections
  1. Prerequisites
  2. Learning Objectives
  3. 1. The Problem
  4. 2. Labels: Name the Loop
  5. 3. continue With a Label
  6. Code Examples
  7. Common Beginner Mistakes
  8. Practice Questions
  9. Practice Answers
  10. Key Takeaways

Lecture 024: Labels

Day9: break, continue & Labels
Lecture024 of 057
Estimated video length10 min
TargetDart 3.12
LevelBeginner

In nested loops, break and continue only affect the innermost loop. To stop searching a whole grid the moment you find an empty seat, you need to leave the outer loop too. A label names a loop so break and continue can target it.

Contents

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


Prerequisites

Lectures 022 (nested loops) and 023 (break and continue).


Learning Objectives

  • Name a loop with a label
  • Leave an outer loop with break label
  • Skip to the outer loop's next round with continue label
  • Know when a label is worth using

1. The Problem

A plain break in the inner loop only ends the inner loop. The outer loop keeps going:

dart
for (var i = 1; i <= 3; i++) {
  for (var j = 1; j <= 3; j++) {
    if (j == 2) break;
    print('plain break: i=$i j=$j');
  }
}

Output

code
plain break: i=1 j=1
plain break: i=2 j=1
plain break: i=3 j=1

2. Labels: Name the Loop

Write a name followed by : just before a loop. Then break name; leaves that loop:

dart
outer:
for (var i = 1; i <= 3; i++) {
  for (var j = 1; j <= 3; j++) {
    if (i == 2 && j == 2) break outer;
    print('break outer: i=$i j=$j');
  }
}

Output

code
break outer: i=1 j=1
break outer: i=1 j=2
break outer: i=1 j=3
break outer: i=2 j=1

At i=2, j=2 both loops stop at once. Round i=3 never happens.

3. continue With a Label

continue name; jumps to the next round of the named loop, abandoning the rest of the inner loop:

dart
rows:
for (var i = 1; i <= 3; i++) {
  for (var j = 1; j <= 3; j++) {
    if (j == 2) continue rows;
    print('continue rows: i=$i j=$j');
  }
}

Output

code
continue rows: i=1 j=1
continue rows: i=2 j=1
continue rows: i=3 j=1
StatementEffect
break;leave the innermost loop
break outer;leave the loop labelled outer (and everything inside it)
continue;next round of the innermost loop
continue outer;next round of the loop labelled outer

Label names follow the same rules as variable names. Pick one that says what the loop goes through: rows, search, shelf.


Code Examples

Example 1: Finding a Seat and Checking Shelves

dart
void main() {
  var seats = [
    ['taken', 'taken', 'free'],
    ['taken', 'free', 'free'],
    ['free', 'free', 'free'],
  ];
  var foundRow = -1;
  var foundCol = -1;

  search:
  for (var r = 0; r < seats.length; r++) {
    for (var c = 0; c < seats[r].length; c++) {
      if (seats[r][c] == 'free') {
        foundRow = r;
        foundCol = c;
        break search;
      }
    }
  }
  print('First free seat: row ${foundRow + 1}, seat ${foundCol + 1}');

  var shelves = [
    ['ok', 'ok', 'ok'],
    ['ok', 'damaged', 'ok'],
    ['ok', 'ok', 'ok'],
  ];
  shelf:
  for (var s = 0; s < shelves.length; s++) {
    for (var item in shelves[s]) {
      if (item == 'damaged') {
        print('Shelf ${s + 1}: needs checking, moving on');
        continue shelf;
      }
    }
    print('Shelf ${s + 1}: all items fine');
  }
}

Output

code
First free seat: row 1, seat 3
Shelf 1: all items fine
Shelf 2: needs checking, moving on
Shelf 3: all items fine

break search stops the whole seat search at the first free seat. continue shelf gives up on a shelf as soon as one damaged item is found, and skips that shelf's "all items fine" line.


Common Beginner Mistakes

1. Breaking to a label that doesn't exist

dart
for (var i = 0; i < 2; i++) {
  break missing;
}
code
Error: Can't break to 'missing'.

The label must be on a loop that contains the break.

2. Putting the label in the wrong place: the label goes directly before the loop it names (outer: then for), not before the inner loop or after the loop.

3. Using labels everywhere: labels are only needed for nested loops. In a single loop, a plain break or continue does the job.


Practice Questions

  1. In nested loops, what does a plain break leave?
  2. Where do you write a label?
  3. What's the difference between break outer; and continue outer;?
  4. In the seat example, if row 1 were all taken, which seat would be found?
  5. Is a label useful in a loop that isn't nested?

Practice Answers

  1. Only the innermost loop.
  2. Just before the loop, followed by a colon: outer: then for (...).
  3. break outer ends the outer loop completely; continue outer moves to the outer loop's next round.
  4. Row 2, seat 2, the first 'free' in the second row.
  5. No. Plain break and continue already target that loop.

Key Takeaways

  • A label (name:) names a loop.
  • break name; leaves that loop, even from inside an inner loop.
  • continue name; jumps to that loop's next round.
  • Use labels only with nested loops, and give them meaningful names.