Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Break, Continue, and Labeled Loops | Loops and Iteration Patterns
Dart Control Flow

bookBreak, Continue, and Labeled Loops

In Dart, you often need to control how loops behave beyond simple iteration. The break and continue statements let you fine-tune a loop's flow, and labels allow you to manage even complex nested loops. By using these tools, you can exit a loop early, skip particular iterations, or break out of multiple levels of nested loops efficiently.

main.dart

main.dart

copy
1234567891011121314151617
void main() { List<int> numbers = [3, 5, 7, -2, 9, 4]; int? firstNegative; for (var number in numbers) { if (number < 0) { firstNegative = number; break; } } if (firstNegative != null) { print('First negative number found: $firstNegative'); } else { print('No negative numbers found.'); } }

The break statement immediately stops the closest enclosing loop. In the example above, as soon as a negative number is found in the list, break exits the for loop, and no further numbers are checked. This is useful when you are searching for a specific value and want to avoid unnecessary iterations once your goal is met.

main.dart

main.dart

copy
1234567891011
void main() { List<int> numbers = [1, 2, 3, 4, 5, 6]; print('Even numbers in the list:'); for (var number in numbers) { if (number % 2 != 0) { continue; } print(number); } }

The continue statement skips the rest of the loop body for the current iteration and proceeds with the next one. In the previous example, whenever an odd number is encountered, continue causes the loop to skip printing that number. This lets you filter which elements are processed, making your code cleaner and more focused on relevant data.

main.dart

main.dart

copy
12345678910111213141516171819202122232425
void main() { List<List<int>> matrix = [ [2, 4, 6], [8, 10, 12], [14, 15, 16] ]; int? foundNumber; outerLoop: for (var row in matrix) { for (var value in row) { if (value % 5 == 0) { foundNumber = value; break outerLoop; } } } if (foundNumber != null) { print('First number divisible by 5 found: $foundNumber'); } else { print('No number divisible by 5 found.'); } }

Labeled loops give you control over which loop is affected by a break or continue statement, especially in nested situations. By assigning a label to a loop, you can use break labelName; to exit not just the innermost loop, but any outer loop with that label. In the matrix search example, the label outerLoop allows the code to break out of both the inner and outer loops as soon as a number divisible by 5 is found. Use labeled loops when you need to escape multiple levels of iteration based on a single condition, keeping your code readable and efficient.

question mark

Which statement about Dart's loop control flow is correct?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 4

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Suggested prompts:

Can you show me an example of using `break` in a Dart loop?

How does `continue` work differently from `break` in Dart?

When should I use labeled loops in Dart?

bookBreak, Continue, and Labeled Loops

Stryg for at vise menuen

In Dart, you often need to control how loops behave beyond simple iteration. The break and continue statements let you fine-tune a loop's flow, and labels allow you to manage even complex nested loops. By using these tools, you can exit a loop early, skip particular iterations, or break out of multiple levels of nested loops efficiently.

main.dart

main.dart

copy
1234567891011121314151617
void main() { List<int> numbers = [3, 5, 7, -2, 9, 4]; int? firstNegative; for (var number in numbers) { if (number < 0) { firstNegative = number; break; } } if (firstNegative != null) { print('First negative number found: $firstNegative'); } else { print('No negative numbers found.'); } }

The break statement immediately stops the closest enclosing loop. In the example above, as soon as a negative number is found in the list, break exits the for loop, and no further numbers are checked. This is useful when you are searching for a specific value and want to avoid unnecessary iterations once your goal is met.

main.dart

main.dart

copy
1234567891011
void main() { List<int> numbers = [1, 2, 3, 4, 5, 6]; print('Even numbers in the list:'); for (var number in numbers) { if (number % 2 != 0) { continue; } print(number); } }

The continue statement skips the rest of the loop body for the current iteration and proceeds with the next one. In the previous example, whenever an odd number is encountered, continue causes the loop to skip printing that number. This lets you filter which elements are processed, making your code cleaner and more focused on relevant data.

main.dart

main.dart

copy
12345678910111213141516171819202122232425
void main() { List<List<int>> matrix = [ [2, 4, 6], [8, 10, 12], [14, 15, 16] ]; int? foundNumber; outerLoop: for (var row in matrix) { for (var value in row) { if (value % 5 == 0) { foundNumber = value; break outerLoop; } } } if (foundNumber != null) { print('First number divisible by 5 found: $foundNumber'); } else { print('No number divisible by 5 found.'); } }

Labeled loops give you control over which loop is affected by a break or continue statement, especially in nested situations. By assigning a label to a loop, you can use break labelName; to exit not just the innermost loop, but any outer loop with that label. In the matrix search example, the label outerLoop allows the code to break out of both the inner and outer loops as soon as a number divisible by 5 is found. Use labeled loops when you need to escape multiple levels of iteration based on a single condition, keeping your code readable and efficient.

question mark

Which statement about Dart's loop control flow is correct?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 4
some-alt