On this page10 sections
Lecture 024: Labels
| Day | 9: break, continue & Labels |
| Lecture | 024 of 057 |
| Estimated video length | 10 min |
| Target | Dart 3.12 |
| Level | Beginner |
In nested loops,
breakandcontinueonly 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 sobreakandcontinuecan 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:
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
plain break: i=1 j=1
plain break: i=2 j=1
plain break: i=3 j=12. Labels: Name the Loop
Write a name followed by : just before a loop. Then break name; leaves that loop:
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
break outer: i=1 j=1
break outer: i=1 j=2
break outer: i=1 j=3
break outer: i=2 j=1At 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:
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
continue rows: i=1 j=1
continue rows: i=2 j=1
continue rows: i=3 j=1| Statement | Effect |
|---|---|
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
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
First free seat: row 1, seat 3
Shelf 1: all items fine
Shelf 2: needs checking, moving on
Shelf 3: all items finebreak 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
for (var i = 0; i < 2; i++) {
break missing;
}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
- In nested loops, what does a plain
breakleave? - Where do you write a label?
- What's the difference between
break outer;andcontinue outer;? - In the seat example, if row 1 were all taken, which seat would be found?
- Is a label useful in a loop that isn't nested?
Practice Answers
- Only the innermost loop.
- Just before the loop, followed by a colon:
outer:thenfor (...). break outerends the outer loop completely;continue outermoves to the outer loop's next round.- Row 2, seat 2, the first
'free'in the second row. - No. Plain
breakandcontinuealready 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.