On this page11 sections
Lecture 023: break & continue
| Day | 9: break, continue & Labels |
| Lecture | 023 of 057 |
| Estimated video length | 12 min |
| Target | Dart 3.12 |
| Level | Beginner |
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.
breakstops a loop early;continueskips 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)withbreaksafely
1. break: Stop the Loop
break ends the loop immediately. The program carries on after the loop.
for (var i = 1; i <= 10; i++) {
if (i == 5) {
break;
}
print(i);
}
print('Loop finished');Output
1
2
3
4
Loop finishedWhen i reaches 5, the loop stops. 5 to 10 are never printed.
2. Searching With break
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 2foundAt 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:
for (var i = 1; i <= 6; i++) {
if (i % 2 == 0) {
continue;
}
print('odd: $i');
}Output
odd: 1
odd: 3
odd: 5var 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| Keyword | Effect |
|---|---|
break | leave the whole loop now |
continue | skip 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:
var n = 0;
while (true) {
n++;
if (n > 3) break;
print('while true: $n');
}Output
while true: 1
while true: 2
while true: 3Make sure the break is always reachable, or you've written an infinite loop (Lecture 021).
Code Examples
Example 1: Processing Orders
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
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 2450Invalid 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
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
if (x > 3) {
break;
}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
- What does this print?
for (var i = 1; i <= 5; i++) { if (i == 3) break; print(i); } - And with
continueinstead ofbreak? - Write a loop over
[4, 7, 0, 9]that stops at the first0. - Write a loop that prints only the names longer than 3 letters, using
continue. - In nested loops, which loop does
breakleave?
Practice Answers
1,21,2,4,5
for (var n in [4, 7, 0, 9]) {
if (n == 0) break;
print(n);
}for (var name in names) {
if (name.length <= 3) continue;
print(name);
}- Only the innermost loop that contains it.
Key Takeaways
breakleaves the loop immediately;continueskips to the next round.- Use
breakto stop a search at the first match. - Use
continueto skip invalid items. while (true)+breakworks if thebreakis always reachable.- In a
whileloop, update the counter before anycontinue.