Break, 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
1234567891011121314151617void 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
1234567891011void 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
12345678910111213141516171819202122232425void 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.
Bedankt voor je feedback!
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
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?
Geweldig!
Completion tarief verbeterd naar 10
Break, Continue, and Labeled Loops
Veeg om het menu te tonen
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
1234567891011121314151617void 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
1234567891011void 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
12345678910111213141516171819202122232425void 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.
Bedankt voor je feedback!