From f60f5b2db1b10b9cc5d18f870f2e8e6b1f656502 Mon Sep 17 00:00:00 2001 From: J-Manoj-06 Date: Mon, 27 Jan 2025 22:16:24 +0530 Subject: [PATCH 01/20] Update using-streams.md for adding YouTube Video This is the change for updating the webpage in the https://dart.dev/libraries/async/using-streams . Adding a Youtube video for the topic streams makes the user better to understand the topic and reduce the difficulties of them. --- src/content/libraries/async/using-streams.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/content/libraries/async/using-streams.md b/src/content/libraries/async/using-streams.md index e89d4c4a36..dfde7ec4e1 100644 --- a/src/content/libraries/async/using-streams.md +++ b/src/content/libraries/async/using-streams.md @@ -13,6 +13,8 @@ js: [{url: '/assets/js/inject_dartpad.js', defer: true}] * There are two kinds of streams: single subscription or broadcast. ::: + + Asynchronous programming in Dart is characterized by the [Future][] and [Stream][] classes. From 876baa7f459131d9e86cc8f55b0ba17c789119bc Mon Sep 17 00:00:00 2001 From: J-Manoj-06 Date: Mon, 27 Jan 2025 22:17:10 +0530 Subject: [PATCH 02/20] Update using-streams.md --- src/content/libraries/async/using-streams.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/content/libraries/async/using-streams.md b/src/content/libraries/async/using-streams.md index dfde7ec4e1..e89d4c4a36 100644 --- a/src/content/libraries/async/using-streams.md +++ b/src/content/libraries/async/using-streams.md @@ -13,8 +13,6 @@ js: [{url: '/assets/js/inject_dartpad.js', defer: true}] * There are two kinds of streams: single subscription or broadcast. ::: - - Asynchronous programming in Dart is characterized by the [Future][] and [Stream][] classes. From ec83957d046ca05b554bec81d11a84b8c4185345 Mon Sep 17 00:00:00 2001 From: Manoj J Date: Sat, 22 Feb 2025 23:36:11 +0530 Subject: [PATCH 03/20] Explaining labels in dart with example. --- src/content/language/branches.md | 277 +++++++++++++++++++++++++++++-- 1 file changed, 261 insertions(+), 16 deletions(-) diff --git a/src/content/language/branches.md b/src/content/language/branches.md index 4f1fee84c9..a5ff57fe90 100644 --- a/src/content/language/branches.md +++ b/src/content/language/branches.md @@ -115,29 +115,274 @@ switch (command) { -Empty cases fall through to the next case, allowing cases to share a body. -For an empty case that does not fall through, -use [`break`][break] for its body. -For non-sequential fall-through, -you can use a [`continue` statement][break] and a label: +### Break in Dart + +The `break` statement in Dart is used to exit a loop or switch statement immediately when a specific condition is met. It helps to improve the efficiency of the code by stopping unnecessary iterations. + +Here is the sample program to use `break` in Dart : - ```dart -switch (command) { - case 'OPEN': - executeOpen(); - continue newCase; // Continues executing at the newCase label. +void main() { + String grade = 'B'; + switch (grade) { + case 'A': + print('Excellent!'); + break; + case 'B': + print('Good Job!'); + break; + case 'C': + print('Needs Improvement'); + break; + default: + print('Invalid Grade'); + } +} - case 'DENIED': // Empty case falls through. - case 'CLOSED': - executeClosed(); // Runs for both DENIED and CLOSED, +``` - newCase: - case 'PENDING': - executeNowClosed(); // Runs for both OPEN and PENDING. +In the above example , the condition is met at `case B`. So, the code stops execution after this case. + +#### `break` in for loop : + +The below code demonstrate the usage of `break` in `for` loop : + +```dart +void main() { + for (int i = 1; i <= 5; i++) { + if (i == 3) { + print('Breaking at i = $i'); + break; + } + print(i); + } + print('Loop ended.'); } ``` +In the above example, the loop stops execution when `i` is `3`. This means that if the necessary condition is met, the loop breaks. So, the output would be : +```dart +1 +2 +Breaking at i = 3 +Loop ended. + +``` + +#### `break` in while loop : + +The below code demonstrate the usage of `break` in `while` loop : + +```dart +void main() { + int i = 1; + while (i <= 5) { + if (i == 4) { + print('Breaking at i = $i'); + break; + } + print(i); + i++; + } +} + +``` +In the above example, the loop stops execution when `i` is `4`. So, the output would be : + +```dart +1 +2 +3 +Breaking at i = 4 +``` + +#### `break` in do while loop : + +The below code demonstrate the usage of `break` in `do while` loop : +```dart +void main() { + int i = 1; + + do { + print('Iteration: $i'); + + if (i == 3) { + print('Breaking the loop at i = $i'); + break; + } + + i++; + } while (i <= 5); + + print('Loop ended.'); +} +``` +In the above example, the loop stops execution when `i` is `3`. So, the output would be : + +```dart +Iteration: 1 +Iteration: 2 +Iteration: 3 +Breaking the loop at i = 3 +Loop ended. +``` + +#### `break` in nested while loop : + +Nested loop is the loop present inside another loop. So, here is the sample program to demonstrate the `break` statement in nested while loop : + +```dart +void main() { + int i = 1; + + while (i <= 3) { + int j = 1; + while (j <= 3) { + if (j == 2) { + break; + } + print('i = $i, j = $j'); + j++; + } + i++; + print(''); + } +} +``` +In the above example, the inner loop is exited whenever the value of `j` is `2`. So, the output would be : + +```dart +i = 1, j = 1 + +i = 2, j = 1 + +i = 3, j = 1 +``` + +### Continue in Dart + +The `continue` statement in Dart is used to skip the current iteration of a loop and proceed to the next iteration. It is particularly useful when you want to ignore certain conditions without breaking the loop. + +#### `continue` in for loop : + +The below code demonstrate the usage of `continue` in `for` loop : + +```dart +void main() { + for (int i = 1; i <= 5; i++) { + if (i == 3) { + continue; + } + print('Iteration: $i'); + } +} + +``` +In the above example, the loop skips the iteration when `i` is `3`. So, the output would be : + +```dart +Iteration: 1 +Iteration: 2 +Iteration: 4 +Iteration: 5 + +``` + +#### `continue` in while loop : + +The below code demonstrate the usage of `continue` in `while` loop : + +```dart +void main() { + int i = 0; + + while (i < 5) { + i++; + if (i == 3) { + continue; + } + + print('Iteration: $i'); + } +} + +``` +In the above example, the loop skips the iteration when `i` is `3`. So, the output would be : + +```dart +Iteration: 1 +Iteration: 2 +Iteration: 4 +Iteration: 5 +``` +#### `continue` in do while loop : + +The below code demonstrate the usage of `continue` in `do while` loop : + +```dart +void main() { + int i = 0; + + do { + i++; + + if (i == 3) { + continue; + } + + print('Iteration: $i'); + } while (i < 5); +} +``` + +In the above example, the loop skips the iteration when `i` is `3`. So, the output would be : + +```dart +Iteration: 1 +Iteration: 2 +Iteration: 4 +Iteration: 5 +``` + +#### `continue` in nested while loop : + +Nested loop is the loop present inside another loop. So, here is the sample program to demonstrate the `continue` statement in nested while loop : + +```dart +void main() { + int i = 1; + + while (i <= 3) { + int j = 1; + while (j <= 3) { + if (j == 2) { + j++; + continue; + } + print('i = $i, j = $j'); + j++; + } + print(''); + i++; + } +} + +``` + +In the above example, the inner loop skips the iteration whenever the value of `j` is `2`. So, the output would be : + +```dart +i = 1, j = 1 +i = 1, j = 3 + +i = 2, j = 1 +i = 2, j = 3 + +i = 3, j = 1 +i = 3, j = 3 + +``` + You can use [logical-or patterns][] to allow cases to share a body or a guard. To learn more about patterns and case clauses, check out the patterns documentation on [Switch statements and expressions][]. From 487e613e7081d1a0c964f67d3134bf21a0cb4445 Mon Sep 17 00:00:00 2001 From: Manoj J Date: Sat, 22 Feb 2025 23:45:59 +0530 Subject: [PATCH 04/20] Update branches.md --- src/content/language/branches.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/content/language/branches.md b/src/content/language/branches.md index a5ff57fe90..b24d14869e 100644 --- a/src/content/language/branches.md +++ b/src/content/language/branches.md @@ -141,7 +141,11 @@ void main() { ``` -In the above example , the condition is met at `case B`. So, the code stops execution after this case. +In the above example , the condition is met at `case B`. So, the code stops execution after this case.Expected output : + +```dart +Good Job! +``` #### `break` in for loop : From 77f711250fd3300b20c300b5e28291803f894c6c Mon Sep 17 00:00:00 2001 From: Manoj J Date: Sun, 23 Feb 2025 00:09:42 +0530 Subject: [PATCH 05/20] changing the branches.dart file --- .../language/lib/control_flow/branches.dart | 171 ++++++++++++++++-- 1 file changed, 158 insertions(+), 13 deletions(-) diff --git a/examples/language/lib/control_flow/branches.dart b/examples/language/lib/control_flow/branches.dart index 052c238322..ed499979c3 100644 --- a/examples/language/lib/control_flow/branches.dart +++ b/examples/language/lib/control_flow/branches.dart @@ -70,22 +70,167 @@ dynamic miscDeclAnalyzedButNotTested() { } { - var command = 'OPEN'; - // #docregion switch-empty - switch (command) { - case 'OPEN': - executeOpen(); - continue newCase; // Continues executing at the newCase label. + // #docregion switch-break + void main() { + String grade = 'B'; + switch (grade) { + case 'A': + print('Excellent!'); + break; + case 'B': + print('Good Job!'); + break; + case 'C': + print('Needs Improvement'); + break; + default: + print('Invalid Grade'); + } + } + // #enddocregion switch-break + } - case 'DENIED': // Empty case falls through. - case 'CLOSED': - executeClosed(); // Runs for both DENIED and CLOSED, + { + // #docregion for-loop-break + void main() { + for (int i = 1; i <= 5; i++) { + if (i == 3) { + print('Breaking at i = $i'); + break; + } + print(i); + } + print('Loop ended.'); + } + // #enddocregion for-loop-break + } - newCase: - case 'PENDING': - executeNowClosed(); // Runs for both OPEN and PENDING. + { + // #docregion while-loop-break + void main() { + int i = 1; + while (i <= 5) { + if (i == 4) { + print('Breaking at i = $i'); + break; + } + print(i); + i++; + } + } + // #enddocregion while-loop-break + } + + { + // #docregion do-while-loop-break + void main() { + int i = 1; + + do { + print('Iteration: $i'); + + if (i == 3) { + print('Breaking the loop at i = $i'); + break; + } + + i++; + } while (i <= 5); + + print('Loop ended.'); + } + // #enddocregion do-while-loop-break + } + + { + // #docregion nested-while-loop-break + void main() { + int i = 1; + + while (i <= 3) { + int j = 1; + while (j <= 3) { + if (j == 2) { + break; + } + print('i = $i, j = $j'); + j++; + } + i++; + print(''); + } + } + // #enddocregion nested-while-loop-break + } + + { + // #docregion for-loop-continue + void main() { + for (int i = 1; i <= 5; i++) { + if (i == 3) { + continue; + } + print('Iteration: $i'); + } + } + // #enddocregion for-loop-continue + } + + { + // #docregion while-loop-continue + void main() { + int i = 0; + + while (i < 5) { + i++; + if (i == 3) { + continue; + } + + print('Iteration: $i'); + } + } + // #enddocregion while-loop-continue + } + + { + // #docregion do-while-loop-continue + void main() { + int i = 0; + + do { + i++; + + if (i == 3) { + continue; + } + + print('Iteration: $i'); + } while (i < 5); + } + // #enddocregion do-while-loop-continue + } + + { + // #docregion nested-while-loop-continue + void main() { + int i = 1; + + while (i <= 3) { + int j = 1; + while (j <= 3) { + if (j == 2) { + j++; + continue; + } + print('i = $i, j = $j'); + j++; + } + print(''); + i++; + } } - // #enddocregion switch-empty + // #enddocregion nested-while-loop-continue } { From 70dab6eeb955973e38e8f4f462ea57b868bf4a2b Mon Sep 17 00:00:00 2001 From: Manoj J Date: Fri, 28 Feb 2025 09:34:39 +0530 Subject: [PATCH 06/20] updated labels documentation in branch.md --- src/content/language/branches.md | 317 +++++++++++++++---------------- 1 file changed, 152 insertions(+), 165 deletions(-) diff --git a/src/content/language/branches.md b/src/content/language/branches.md index b24d14869e..6b9f9dad49 100644 --- a/src/content/language/branches.md +++ b/src/content/language/branches.md @@ -115,276 +115,263 @@ switch (command) { -### Break in Dart +Empty cases fall through to the next case, allowing cases to share a body. +For an empty case that does not fall through, +use [`break`][break] for its body. +For non-sequential fall-through, +you can use a [`continue` statement][break] and a label: -The `break` statement in Dart is used to exit a loop or switch statement immediately when a specific condition is met. It helps to improve the efficiency of the code by stopping unnecessary iterations. + +```dart +switch (command) { + case 'OPEN': + executeOpen(); + continue newCase; // Continues executing at the newCase label. -Here is the sample program to use `break` in Dart : + case 'DENIED': // Empty case falls through. + case 'CLOSED': + executeClosed(); // Runs for both DENIED and CLOSED, -```dart -void main() { - String grade = 'B'; - switch (grade) { - case 'A': - print('Excellent!'); - break; - case 'B': - print('Good Job!'); - break; - case 'C': - print('Needs Improvement'); - break; - default: - print('Invalid Grade'); - } + newCase: + case 'PENDING': + executeNowClosed(); // Runs for both OPEN and PENDING. } - ``` -In the above example , the condition is met at `case B`. So, the code stops execution after this case.Expected output : +### Labels in Dart + +In Dart, labels are used to control nested loops(Loop inside Loop) using break and continue. Labels allow you to specify which loop to `break` or `continue`, rather than affecting the innermost loop by default. + +Here is the syntax for labels in Dart : ```dart -Good Job! + +labelName: +for (/* condition */) { + // code +} + ``` -#### `break` in for loop : +A label is simply an identifier followed by a colon (`:`) placed before a loop or statement. + +#### Labels in for loop using `break` : -The below code demonstrate the usage of `break` in `for` loop : +The below code demonstrates the usage of a labels in `for` loop with `break` : ```dart void main() { - for (int i = 1; i <= 5; i++) { - if (i == 3) { - print('Breaking at i = $i'); - break; + outerLoop: + for (int i = 1; i <= 3; i++) { + for (int j = 1; j <= 3; j++) { + print("i = $i, j = $j"); + if (i == 2 && j == 2) { + break outerLoop; + } } - print(i); } - print('Loop ended.'); + print("outerLoop exited"); } + ``` -In the above example, the loop stops execution when `i` is `3`. This means that if the necessary condition is met, the loop breaks. So, the output would be : +In the above example, When `i == 2` and `j == 2`, `break outerLoop;` statement stops both inner and outer loops. So, the expected output would be : + ```dart -1 -2 -Breaking at i = 3 -Loop ended. + +i = 1, j = 1 +i = 1, j = 2 +i = 1, j = 3 +i = 2, j = 1 +i = 2, j = 2 +outerLoop exited ``` -#### `break` in while loop : +#### Labels in for loop using `contiue` : -The below code demonstrate the usage of `break` in `while` loop : +The below code demonstrates the usage of labels in `for` loop with `continue` : ```dart + void main() { - int i = 1; - while (i <= 5) { - if (i == 4) { - print('Breaking at i = $i'); - break; + outerLoop: + for (int i = 1; i <= 3; i++) { + for (int j = 1; j <= 3; j++) { + if (i == 2 && j == 2) { + continue outerLoop; + } + print("i = $i, j = $j"); } - print(i); - i++; } } ``` -In the above example, the loop stops execution when `i` is `4`. So, the output would be : +In the above example, When `i == 2` and `j == 2`, `continue outerLoop;` skips the rest of the iterations for `i = 2` and moves to `i = 3`. So, the output would be : ```dart -1 -2 -3 -Breaking at i = 4 -``` - -#### `break` in do while loop : - -The below code demonstrate the usage of `break` in `do while` loop : -```dart -void main() { - int i = 1; - - do { - print('Iteration: $i'); - - if (i == 3) { - print('Breaking the loop at i = $i'); - break; - } - - i++; - } while (i <= 5); - print('Loop ended.'); -} -``` -In the above example, the loop stops execution when `i` is `3`. So, the output would be : +i = 1, j = 1 +i = 1, j = 2 +i = 1, j = 3 +i = 2, j = 1 +i = 3, j = 1 +i = 3, j = 2 +i = 3, j = 3 -```dart -Iteration: 1 -Iteration: 2 -Iteration: 3 -Breaking the loop at i = 3 -Loop ended. ``` -#### `break` in nested while loop : +#### Labels in while loop using `break` : -Nested loop is the loop present inside another loop. So, here is the sample program to demonstrate the `break` statement in nested while loop : +The below code demonstrates the usage of a labels in `while` loop with `break` : ```dart + void main() { int i = 1; - + outerLoop: while (i <= 3) { int j = 1; while (j <= 3) { - if (j == 2) { - break; + print("i = $i, j = $j"); + if (i == 2 && j == 2) { + break outerLoop; } - print('i = $i, j = $j'); j++; } i++; - print(''); } + print("outerLoop exited"); } + ``` -In the above example, the inner loop is exited whenever the value of `j` is `2`. So, the output would be : + +In the above example, the program breaks out of both inner and outer while loops when `i == 2` and `j == 2`.So, the expected output would be : ```dart -i = 1, j = 1 +i = 1, j = 1 +i = 1, j = 2 +i = 1, j = 3 i = 2, j = 1 +i = 2, j = 2 +outerLoop exited -i = 3, j = 1 ``` -### Continue in Dart - -The `continue` statement in Dart is used to skip the current iteration of a loop and proceed to the next iteration. It is particularly useful when you want to ignore certain conditions without breaking the loop. +#### Labels in while loop using `continue`: -#### `continue` in for loop : - -The below code demonstrate the usage of `continue` in `for` loop : +The below code demonstrates the usage of labels in `while` loop using `continue`: ```dart + void main() { - for (int i = 1; i <= 5; i++) { - if (i == 3) { - continue; + int i = 1; + + outerLoop: + while (i <= 3) { + int j = 1; + while (j <= 3) { + if (i == 2 && j == 2) { + i++; + continue outerLoop; + } + print("i = $i, j = $j"); + j++; } - print('Iteration: $i'); + i++; } } ``` -In the above example, the loop skips the iteration when `i` is `3`. So, the output would be : +In the above example, the iteration for i = 2, j = 2 is skipped, and the loop moves directly to i = 3. As a result, the output would be: ```dart -Iteration: 1 -Iteration: 2 -Iteration: 4 -Iteration: 5 +i = 1, j = 1 +i = 1, j = 2 +i = 1, j = 3 +i = 2, j = 1 +i = 3, j = 1 +i = 3, j = 2 +i = 3, j = 3 + ``` -#### `continue` in while loop : - -The below code demonstrate the usage of `continue` in `while` loop : +#### Label in do while loop using `break` : +The below code demonstrate the usage of label in `do while` loop : ```dart -void main() { - int i = 0; - while (i < 5) { +void main() { + int i = 1; + outerLoop: + do { + int j = 1; + do { + print("i = $i, j = $j"); + if (i == 2 && j == 2) { + break outerLoop; + } + j++; + } while (j <= 3); i++; - if (i == 3) { - continue; - } + } while (i <= 3); - print('Iteration: $i'); - } + print("outerLoop exited"); } ``` -In the above example, the loop skips the iteration when `i` is `3`. So, the output would be : - -```dart -Iteration: 1 -Iteration: 2 -Iteration: 4 -Iteration: 5 -``` -#### `continue` in do while loop : - -The below code demonstrate the usage of `continue` in `do while` loop : +In the above example, the program breaks out of both inner and outer loops when `i == 2` and `j == 2`. So, the expected output would be : ```dart -void main() { - int i = 0; - - do { - i++; - if (i == 3) { - continue; - } - - print('Iteration: $i'); - } while (i < 5); -} -``` - -In the above example, the loop skips the iteration when `i` is `3`. So, the output would be : +i = 1, j = 1 +i = 1, j = 2 +i = 1, j = 3 +i = 2, j = 1 +i = 2, j = 2 +outerLoop exited -```dart -Iteration: 1 -Iteration: 2 -Iteration: 4 -Iteration: 5 ``` -#### `continue` in nested while loop : +#### Labels in do while loop using `continue` : -Nested loop is the loop present inside another loop. So, here is the sample program to demonstrate the `continue` statement in nested while loop : +The below code demonstrates the usage of labels in `do while` loop using `continue`: ```dart void main() { int i = 1; - while (i <= 3) { + outerLoop: + do { int j = 1; - while (j <= 3) { - if (j == 2) { - j++; - continue; + do { + if (i == 2 && j == 2) { + i++; + continue outerLoop; } - print('i = $i, j = $j'); + print("i = $i, j = $j"); j++; - } - print(''); + } while (j <= 3); i++; - } + } while (i <= 3); } ``` -In the above example, the inner loop skips the iteration whenever the value of `j` is `2`. So, the output would be : +In the above example, the loop skips i = 2, j = 2 and moves directly to i = 3. As a result, the output would be: ```dart -i = 1, j = 1 -i = 1, j = 3 - -i = 2, j = 1 -i = 2, j = 3 - -i = 3, j = 1 -i = 3, j = 3 +i = 1, j = 1 +i = 1, j = 2 +i = 1, j = 3 +i = 2, j = 1 +i = 3, j = 1 +i = 3, j = 2 +i = 3, j = 3 + ``` You can use [logical-or patterns][] to allow cases to share a body or a guard. From 2449d12cca72bfa527d0d3fa2ca30cde650e5101 Mon Sep 17 00:00:00 2001 From: Manoj J Date: Fri, 28 Feb 2025 09:49:56 +0530 Subject: [PATCH 07/20] Updated label in branches.dart --- .../language/lib/control_flow/branches.dart | 215 ++++++++---------- 1 file changed, 100 insertions(+), 115 deletions(-) diff --git a/examples/language/lib/control_flow/branches.dart b/examples/language/lib/control_flow/branches.dart index ed499979c3..7a68364295 100644 --- a/examples/language/lib/control_flow/branches.dart +++ b/examples/language/lib/control_flow/branches.dart @@ -70,167 +70,152 @@ dynamic miscDeclAnalyzedButNotTested() { } { - // #docregion switch-break - void main() { - String grade = 'B'; - switch (grade) { - case 'A': - print('Excellent!'); - break; - case 'B': - print('Good Job!'); - break; - case 'C': - print('Needs Improvement'); - break; - default: - print('Invalid Grade'); - } + var command = 'OPEN'; + // #docregion switch-empty + switch (command) { + case 'OPEN': + executeOpen(); + continue newCase; // Continues executing at the newCase label. + + case 'DENIED': // Empty case falls through. + case 'CLOSED': + executeClosed(); // Runs for both DENIED and CLOSED, + + newCase: + case 'PENDING': + executeNowClosed(); // Runs for both OPEN and PENDING. } - // #enddocregion switch-break + // #enddocregion switch-empty } { - // #docregion for-loop-break - void main() { - for (int i = 1; i <= 5; i++) { - if (i == 3) { - print('Breaking at i = $i'); - break; - } - print(i); - } - print('Loop ended.'); - } - // #enddocregion for-loop-break + // #docregion syntax-label + labelName: + for (/* condition */) { + // code + } + // #enddocregion syntax-label } { - // #docregion while-loop-break + // #docregion label-for-loop-break void main() { - int i = 1; - while (i <= 5) { - if (i == 4) { - print('Breaking at i = $i'); - break; - } - print(i); - i++; + outerLoop: + for (int i = 1; i <= 3; i++) { + for (int j = 1; j <= 3; j++) { + print("i = $i, j = $j"); + if (i == 2 && j == 2) { + break outerLoop; + } } } - // #enddocregion while-loop-break + print("outerLoop exited"); + } + // #enddocregion label-for-loop-break } { - // #docregion do-while-loop-break + // #docregion label-for-loop-continue void main() { - int i = 1; - - do { - print('Iteration: $i'); - - if (i == 3) { - print('Breaking the loop at i = $i'); - break; + outerLoop: + for (int i = 1; i <= 3; i++) { + for (int j = 1; j <= 3; j++) { + if (i == 2 && j == 2) { + continue outerLoop; + } + print("i = $i, j = $j"); + } } - - i++; - } while (i <= 5); - - print('Loop ended.'); } - // #enddocregion do-while-loop-break + // #enddocregion label-for-loop-continue } { - // #docregion nested-while-loop-break - void main() { + // #docregion label-while-loop-break + void main() { int i = 1; - + outerLoop: while (i <= 3) { int j = 1; while (j <= 3) { - if (j == 2) { - break; + print("i = $i, j = $j"); + if (i == 2 && j == 2) { + break outerLoop; } - print('i = $i, j = $j'); j++; } i++; - print(''); - } } - // #enddocregion nested-while-loop-break - } - - { - // #docregion for-loop-continue - void main() { - for (int i = 1; i <= 5; i++) { - if (i == 3) { - continue; - } - print('Iteration: $i'); - } + print("outerLoop exited"); } - // #enddocregion for-loop-continue + // #enddocregion label-while-loop-break } { - // #docregion while-loop-continue + // #docregion label-while-loop-continue void main() { - int i = 0; - - while (i < 5) { - i++; - if (i == 3) { - continue; - } - - print('Iteration: $i'); + int i = 1; + + outerLoop: + while (i <= 3) { + int j = 1; + while (j <= 3) { + if (i == 2 && j == 2) { + i++; + continue outerLoop; + } + print("i = $i, j = $j"); + j++; + } + i++; } } - // #enddocregion while-loop-continue + // #enddocregion label-while-loop-continue } { - // #docregion do-while-loop-continue + // #docregion label-do-while-loop-break + void main() { - int i = 0; - - do { - i++; - - if (i == 3) { - continue; - } - - print('Iteration: $i'); - } while (i < 5); + int i = 1; + outerLoop: + do { + int j = 1; + do { + print("i = $i, j = $j"); + if (i == 2 && j == 2) { + break outerLoop; + } + j++; + } while (j <= 3); + i++; + } while (i <= 3); + + print("outerLoop exited"); } - // #enddocregion do-while-loop-continue + // #enddocregion label-do-while-loop-break } { - // #docregion nested-while-loop-continue + // #docregion label-do-while-loop-continue void main() { - int i = 1; - - while (i <= 3) { - int j = 1; - while (j <= 3) { - if (j == 2) { + int i = 1; + + outerLoop: + do { + int j = 1; + do { + if (i == 2 && j == 2) { + i++; + continue outerLoop; + } + print("i = $i, j = $j"); j++; - continue; - } - print('i = $i, j = $j'); - j++; - } - print(''); - i++; - } + } while (j <= 3); + i++; + } while (i <= 3); } - // #enddocregion nested-while-loop-continue + // #enddocregion label-do-while-loop-continue } { From 47b620146b83d700d05a1209335d2cbd7726bd2c Mon Sep 17 00:00:00 2001 From: Manoj J Date: Fri, 28 Feb 2025 14:00:05 +0530 Subject: [PATCH 08/20] Updated the error in branches.md --- src/content/language/branches.md | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/content/language/branches.md b/src/content/language/branches.md index 6b9f9dad49..8b3a0372a4 100644 --- a/src/content/language/branches.md +++ b/src/content/language/branches.md @@ -142,17 +142,6 @@ switch (command) { In Dart, labels are used to control nested loops(Loop inside Loop) using break and continue. Labels allow you to specify which loop to `break` or `continue`, rather than affecting the innermost loop by default. -Here is the syntax for labels in Dart : - -```dart - -labelName: -for (/* condition */) { - // code -} - -``` - A label is simply an identifier followed by a colon (`:`) placed before a loop or statement. #### Labels in for loop using `break` : From 7d0f79067afd27163e59f188a88124a7f6cc6873 Mon Sep 17 00:00:00 2001 From: Manoj J Date: Fri, 28 Feb 2025 14:02:08 +0530 Subject: [PATCH 09/20] Updated error in branches.dart --- examples/language/lib/control_flow/branches.dart | 9 --------- 1 file changed, 9 deletions(-) diff --git a/examples/language/lib/control_flow/branches.dart b/examples/language/lib/control_flow/branches.dart index 7a68364295..12fdbe4430 100644 --- a/examples/language/lib/control_flow/branches.dart +++ b/examples/language/lib/control_flow/branches.dart @@ -88,15 +88,6 @@ dynamic miscDeclAnalyzedButNotTested() { // #enddocregion switch-empty } - { - // #docregion syntax-label - labelName: - for (/* condition */) { - // code - } - // #enddocregion syntax-label - } - { // #docregion label-for-loop-break void main() { From 3344b7b670f343518125d60dd49e418d2b107ba8 Mon Sep 17 00:00:00 2001 From: Manoj J Date: Fri, 28 Feb 2025 14:05:12 +0530 Subject: [PATCH 10/20] Updated error in branches.md --- src/content/language/branches.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/content/language/branches.md b/src/content/language/branches.md index 8b3a0372a4..ac7219be83 100644 --- a/src/content/language/branches.md +++ b/src/content/language/branches.md @@ -142,6 +142,15 @@ switch (command) { In Dart, labels are used to control nested loops(Loop inside Loop) using break and continue. Labels allow you to specify which loop to `break` or `continue`, rather than affecting the innermost loop by default. +Here is the syntax for labels in Dart : + +```dart +labelName: +for (/* condition */) { + // code +} +``` + A label is simply an identifier followed by a colon (`:`) placed before a loop or statement. #### Labels in for loop using `break` : From d3a1b40a8e4db1e3a08a79bf5e86ae14bcc0ff72 Mon Sep 17 00:00:00 2001 From: Manoj J Date: Mon, 3 Mar 2025 20:48:11 +0530 Subject: [PATCH 11/20] removed from branches.md --- src/content/language/branches.md | 234 ------------------------------- 1 file changed, 234 deletions(-) diff --git a/src/content/language/branches.md b/src/content/language/branches.md index ac7219be83..4f1fee84c9 100644 --- a/src/content/language/branches.md +++ b/src/content/language/branches.md @@ -138,240 +138,6 @@ switch (command) { } ``` -### Labels in Dart - -In Dart, labels are used to control nested loops(Loop inside Loop) using break and continue. Labels allow you to specify which loop to `break` or `continue`, rather than affecting the innermost loop by default. - -Here is the syntax for labels in Dart : - -```dart -labelName: -for (/* condition */) { - // code -} -``` - -A label is simply an identifier followed by a colon (`:`) placed before a loop or statement. - -#### Labels in for loop using `break` : - -The below code demonstrates the usage of a labels in `for` loop with `break` : - -```dart -void main() { - outerLoop: - for (int i = 1; i <= 3; i++) { - for (int j = 1; j <= 3; j++) { - print("i = $i, j = $j"); - if (i == 2 && j == 2) { - break outerLoop; - } - } - } - print("outerLoop exited"); -} - -``` - -In the above example, When `i == 2` and `j == 2`, `break outerLoop;` statement stops both inner and outer loops. So, the expected output would be : - -```dart - -i = 1, j = 1 -i = 1, j = 2 -i = 1, j = 3 -i = 2, j = 1 -i = 2, j = 2 -outerLoop exited - -``` - -#### Labels in for loop using `contiue` : - -The below code demonstrates the usage of labels in `for` loop with `continue` : - -```dart - -void main() { - outerLoop: - for (int i = 1; i <= 3; i++) { - for (int j = 1; j <= 3; j++) { - if (i == 2 && j == 2) { - continue outerLoop; - } - print("i = $i, j = $j"); - } - } -} - -``` -In the above example, When `i == 2` and `j == 2`, `continue outerLoop;` skips the rest of the iterations for `i = 2` and moves to `i = 3`. So, the output would be : - -```dart - -i = 1, j = 1 -i = 1, j = 2 -i = 1, j = 3 -i = 2, j = 1 -i = 3, j = 1 -i = 3, j = 2 -i = 3, j = 3 - -``` - -#### Labels in while loop using `break` : - -The below code demonstrates the usage of a labels in `while` loop with `break` : - -```dart - -void main() { - int i = 1; - outerLoop: - while (i <= 3) { - int j = 1; - while (j <= 3) { - print("i = $i, j = $j"); - if (i == 2 && j == 2) { - break outerLoop; - } - j++; - } - i++; - } - print("outerLoop exited"); -} - -``` - -In the above example, the program breaks out of both inner and outer while loops when `i == 2` and `j == 2`.So, the expected output would be : - -```dart - -i = 1, j = 1 -i = 1, j = 2 -i = 1, j = 3 -i = 2, j = 1 -i = 2, j = 2 -outerLoop exited - -``` - -#### Labels in while loop using `continue`: - -The below code demonstrates the usage of labels in `while` loop using `continue`: - -```dart - -void main() { - int i = 1; - - outerLoop: - while (i <= 3) { - int j = 1; - while (j <= 3) { - if (i == 2 && j == 2) { - i++; - continue outerLoop; - } - print("i = $i, j = $j"); - j++; - } - i++; - } -} - -``` -In the above example, the iteration for i = 2, j = 2 is skipped, and the loop moves directly to i = 3. As a result, the output would be: - -```dart - -i = 1, j = 1 -i = 1, j = 2 -i = 1, j = 3 -i = 2, j = 1 -i = 3, j = 1 -i = 3, j = 2 -i = 3, j = 3 - -``` - -#### Label in do while loop using `break` : - -The below code demonstrate the usage of label in `do while` loop : -```dart - -void main() { - int i = 1; - outerLoop: - do { - int j = 1; - do { - print("i = $i, j = $j"); - if (i == 2 && j == 2) { - break outerLoop; - } - j++; - } while (j <= 3); - i++; - } while (i <= 3); - - print("outerLoop exited"); -} - -``` -In the above example, the program breaks out of both inner and outer loops when `i == 2` and `j == 2`. So, the expected output would be : - -```dart - -i = 1, j = 1 -i = 1, j = 2 -i = 1, j = 3 -i = 2, j = 1 -i = 2, j = 2 -outerLoop exited - -``` - -#### Labels in do while loop using `continue` : - -The below code demonstrates the usage of labels in `do while` loop using `continue`: - -```dart -void main() { - int i = 1; - - outerLoop: - do { - int j = 1; - do { - if (i == 2 && j == 2) { - i++; - continue outerLoop; - } - print("i = $i, j = $j"); - j++; - } while (j <= 3); - i++; - } while (i <= 3); -} - -``` - -In the above example, the loop skips i = 2, j = 2 and moves directly to i = 3. As a result, the output would be: - -```dart - -i = 1, j = 1 -i = 1, j = 2 -i = 1, j = 3 -i = 2, j = 1 -i = 3, j = 1 -i = 3, j = 2 -i = 3, j = 3 - -``` - You can use [logical-or patterns][] to allow cases to share a body or a guard. To learn more about patterns and case clauses, check out the patterns documentation on [Switch statements and expressions][]. From 84dbbc0b3d8c36f70c75087a22188bb473c1fad8 Mon Sep 17 00:00:00 2001 From: Manoj J Date: Mon, 3 Mar 2025 21:04:55 +0530 Subject: [PATCH 12/20] Updated loops.md --- src/content/language/loops.md | 234 ++++++++++++++++++++++++++++++++++ 1 file changed, 234 insertions(+) diff --git a/src/content/language/loops.md b/src/content/language/loops.md index 9c5e5df86a..db505c6752 100644 --- a/src/content/language/loops.md +++ b/src/content/language/loops.md @@ -142,6 +142,240 @@ candidates .forEach((c) => c.interview()); ``` +### Labels + +In Dart, labels are used to manage control flow in nested loops using `break` and `continue` statements. They allow you to specify which loop to break out of or continue, rather than affecting the innermost loop by default. + +Here is the syntax for labels in Dart: + +```dart +labelName: +for (/* condition */) { + // code +} +``` + +A label is simply an identifier followed by a colon (`:`) placed before a loop or statement. + +#### Labels in for loop using `break`: + +The below code demonstrates the usage of a labels in `for` loop with `break`: + +```dart +void main() { + outerLoop: + for (int i = 1; i <= 3; i++) { + for (int j = 1; j <= 3; j++) { + print("i = $i, j = $j"); + if (i == 2 && j == 2) { + break outerLoop; + } + } + } + print("outerLoop exited"); +} + +``` + +In the above example, when `i == 2` and `j == 2`, `break outerLoop;` statement stops both inner and outer loops. So, the expected output would be: + +```dart + +i = 1, j = 1 +i = 1, j = 2 +i = 1, j = 3 +i = 2, j = 1 +i = 2, j = 2 +outerLoop exited + +``` + +#### Labels in for loop using `continue`: + +The below code demonstrates the usage of labels in `for` loop with `continue`: + +```dart + +void main() { + outerLoop: + for (int i = 1; i <= 3; i++) { + for (int j = 1; j <= 3; j++) { + if (i == 2 && j == 2) { + continue outerLoop; + } + print("i = $i, j = $j"); + } + } +} + +``` +In the above example, when `i == 2` and `j == 2`, `continue outerLoop;` skips the rest of the iterations for `i = 2` and moves to `i = 3`. So, the output would be: + +```dart + +i = 1, j = 1 +i = 1, j = 2 +i = 1, j = 3 +i = 2, j = 1 +i = 3, j = 1 +i = 3, j = 2 +i = 3, j = 3 + +``` + +#### Labels in while loop using `break`: + +The below code demonstrates the usage of a labels in `while` loop with `break`: + +```dart + +void main() { + int i = 1; + outerLoop: + while (i <= 3) { + int j = 1; + while (j <= 3) { + print("i = $i, j = $j"); + if (i == 2 && j == 2) { + break outerLoop; + } + j++; + } + i++; + } + print("outerLoop exited"); +} + +``` + +In the above example, the program breaks out of both inner and outer while loops when `i == 2` and `j == 2`.So, the expected output would be: + +```dart + +i = 1, j = 1 +i = 1, j = 2 +i = 1, j = 3 +i = 2, j = 1 +i = 2, j = 2 +outerLoop exited + +``` + +#### Labels in while loop using `continue`: + +The below code demonstrates the usage of labels in `while` loop using `continue`: + +```dart + +void main() { + int i = 1; + + outerLoop: + while (i <= 3) { + int j = 1; + while (j <= 3) { + if (i == 2 && j == 2) { + i++; + continue outerLoop; + } + print("i = $i, j = $j"); + j++; + } + i++; + } +} + +``` +In the above example, the iteration for `i = 2` and `j = 2` is skipped, and the loop moves directly to `i = 3`. As a result, the output would be: + +```dart + +i = 1, j = 1 +i = 1, j = 2 +i = 1, j = 3 +i = 2, j = 1 +i = 3, j = 1 +i = 3, j = 2 +i = 3, j = 3 + +``` + +#### Label in do while loop using `break`: + +The below code demonstrate the usage of label in `do while` loop: +```dart + +void main() { + int i = 1; + outerLoop: + do { + int j = 1; + do { + print("i = $i, j = $j"); + if (i == 2 && j == 2) { + break outerLoop; + } + j++; + } while (j <= 3); + i++; + } while (i <= 3); + + print("outerLoop exited"); +} + +``` +In the above example, the program breaks out of both inner and outer loops when `i == 2` and `j == 2`. So, the expected output would be: + +```dart + +i = 1, j = 1 +i = 1, j = 2 +i = 1, j = 3 +i = 2, j = 1 +i = 2, j = 2 +outerLoop exited + +``` + +#### Labels in do while loop using `continue`: + +The below code demonstrates the usage of labels in `do while` loop using `continue`: + +```dart +void main() { + int i = 1; + + outerLoop: + do { + int j = 1; + do { + if (i == 2 && j == 2) { + i++; + continue outerLoop; + } + print("i = $i, j = $j"); + j++; + } while (j <= 3); + i++; + } while (i <= 3); +} + +``` + +In the above example, the loop skips `i = 2` and `j = 2` and moves directly to `i = 3`. As a result, the output would be: + +```dart + +i = 1, j = 1 +i = 1, j = 2 +i = 1, j = 3 +i = 2, j = 1 +i = 3, j = 1 +i = 3, j = 2 +i = 3, j = 3 + +``` + [exceptions]: /language/error-handling [branching]: /language/branches [iteration]: /libraries/dart-core#iteration From 605e025dd71f2202a61293b59143de4286b05531 Mon Sep 17 00:00:00 2001 From: Manoj J Date: Mon, 3 Mar 2025 21:08:56 +0530 Subject: [PATCH 13/20] updated branches.dart --- .../language/lib/control_flow/branches.dart | 121 ------------------ 1 file changed, 121 deletions(-) diff --git a/examples/language/lib/control_flow/branches.dart b/examples/language/lib/control_flow/branches.dart index 12fdbe4430..052c238322 100644 --- a/examples/language/lib/control_flow/branches.dart +++ b/examples/language/lib/control_flow/branches.dart @@ -88,127 +88,6 @@ dynamic miscDeclAnalyzedButNotTested() { // #enddocregion switch-empty } - { - // #docregion label-for-loop-break - void main() { - outerLoop: - for (int i = 1; i <= 3; i++) { - for (int j = 1; j <= 3; j++) { - print("i = $i, j = $j"); - if (i == 2 && j == 2) { - break outerLoop; - } - } - } - print("outerLoop exited"); - } - // #enddocregion label-for-loop-break - } - - { - // #docregion label-for-loop-continue - void main() { - outerLoop: - for (int i = 1; i <= 3; i++) { - for (int j = 1; j <= 3; j++) { - if (i == 2 && j == 2) { - continue outerLoop; - } - print("i = $i, j = $j"); - } - } - } - // #enddocregion label-for-loop-continue - } - - { - // #docregion label-while-loop-break - void main() { - int i = 1; - outerLoop: - while (i <= 3) { - int j = 1; - while (j <= 3) { - print("i = $i, j = $j"); - if (i == 2 && j == 2) { - break outerLoop; - } - j++; - } - i++; - } - print("outerLoop exited"); - } - // #enddocregion label-while-loop-break - } - - { - // #docregion label-while-loop-continue - void main() { - int i = 1; - - outerLoop: - while (i <= 3) { - int j = 1; - while (j <= 3) { - if (i == 2 && j == 2) { - i++; - continue outerLoop; - } - print("i = $i, j = $j"); - j++; - } - i++; - } - } - // #enddocregion label-while-loop-continue - } - - { - // #docregion label-do-while-loop-break - - void main() { - int i = 1; - outerLoop: - do { - int j = 1; - do { - print("i = $i, j = $j"); - if (i == 2 && j == 2) { - break outerLoop; - } - j++; - } while (j <= 3); - i++; - } while (i <= 3); - - print("outerLoop exited"); - } - // #enddocregion label-do-while-loop-break - } - - { - // #docregion label-do-while-loop-continue - void main() { - int i = 1; - - outerLoop: - do { - int j = 1; - do { - if (i == 2 && j == 2) { - i++; - continue outerLoop; - } - print("i = $i, j = $j"); - j++; - } while (j <= 3); - i++; - } while (i <= 3); - } - // #enddocregion label-do-while-loop-continue - } - { dynamic charCode; const slash = '/'; From 099bc6239d3f22e73f16a1235518d033938c744c Mon Sep 17 00:00:00 2001 From: Manoj J Date: Mon, 3 Mar 2025 21:14:18 +0530 Subject: [PATCH 14/20] Documentation updated in loops.dart --- examples/language/lib/control_flow/loops.dart | 122 ++++++++++++++++++ 1 file changed, 122 insertions(+) diff --git a/examples/language/lib/control_flow/loops.dart b/examples/language/lib/control_flow/loops.dart index 7392f3117b..dd4d1ccc0f 100644 --- a/examples/language/lib/control_flow/loops.dart +++ b/examples/language/lib/control_flow/loops.dart @@ -74,4 +74,126 @@ void miscDeclAnalyzedButNotTested() { .forEach((c) => c.interview()); // #enddocregion where } + + { + // #docregion label-for-loop-break + void main() { + outerLoop: + for (int i = 1; i <= 3; i++) { + for (int j = 1; j <= 3; j++) { + print("i = $i, j = $j"); + if (i == 2 && j == 2) { + break outerLoop; + } + } + } + print("outerLoop exited"); + } + // #enddocregion label-for-loop-break + } + + { + // #docregion label-for-loop-continue + void main() { + outerLoop: + for (int i = 1; i <= 3; i++) { + for (int j = 1; j <= 3; j++) { + if (i == 2 && j == 2) { + continue outerLoop; + } + print("i = $i, j = $j"); + } + } + } + // #enddocregion label-for-loop-continue + } + + { + // #docregion label-while-loop-break + void main() { + int i = 1; + outerLoop: + while (i <= 3) { + int j = 1; + while (j <= 3) { + print("i = $i, j = $j"); + if (i == 2 && j == 2) { + break outerLoop; + } + j++; + } + i++; + } + print("outerLoop exited"); + } + // #enddocregion label-while-loop-break + } + + { + // #docregion label-while-loop-continue + void main() { + int i = 1; + + outerLoop: + while (i <= 3) { + int j = 1; + while (j <= 3) { + if (i == 2 && j == 2) { + i++; + continue outerLoop; + } + print("i = $i, j = $j"); + j++; + } + i++; + } + } + // #enddocregion label-while-loop-continue + } + + { + // #docregion label-do-while-loop-break + + void main() { + int i = 1; + outerLoop: + do { + int j = 1; + do { + print("i = $i, j = $j"); + if (i == 2 && j == 2) { + break outerLoop; + } + j++; + } while (j <= 3); + i++; + } while (i <= 3); + + print("outerLoop exited"); + } + // #enddocregion label-do-while-loop-break + } + + { + // #docregion label-do-while-loop-continue + void main() { + int i = 1; + + outerLoop: + do { + int j = 1; + do { + if (i == 2 && j == 2) { + i++; + continue outerLoop; + } + print("i = $i, j = $j"); + j++; + } while (j <= 3); + i++; + } while (i <= 3); + } + // #enddocregion label-do-while-loop-continue + } + } From 1ef41875e0b5c92f74316283ee54085bef2ad98d Mon Sep 17 00:00:00 2001 From: Amanda Fitch <18406675+antfitch@users.noreply.github.com> Date: Mon, 3 Mar 2025 13:07:21 -0800 Subject: [PATCH 15/20] Update loops.md Added editorial changes. --- src/content/language/loops.md | 89 +++++++++++++++++------------------ 1 file changed, 44 insertions(+), 45 deletions(-) diff --git a/src/content/language/loops.md b/src/content/language/loops.md index db505c6752..805b6ff9cf 100644 --- a/src/content/language/loops.md +++ b/src/content/language/loops.md @@ -144,22 +144,28 @@ candidates ### Labels -In Dart, labels are used to manage control flow in nested loops using `break` and `continue` statements. They allow you to specify which loop to break out of or continue, rather than affecting the innermost loop by default. +A label is an identifier followed by a colon (`labelName:`) +that you can place before a statement to create a +_labeled statement_. Loops and switch cases are often used as +labeled statements. A labeled statement can be referenced later +in a `break` or `continue` statement as follows: -Here is the syntax for labels in Dart: +* `break labelName;`: Terminates the execution of the labeled statement. + This is useful for breaking out of a specific outer loop when you're + within a nested loop. -```dart -labelName: -for (/* condition */) { - // code -} -``` +* `continue labelName;`: Skips the rest of the current iteration of the + labeled statement loop and continues with the next iteration. -A label is simply an identifier followed by a colon (`:`) placed before a loop or statement. +Labels are used to manage control flow. They are often used with +loops and switch cases and allow you to specify which statement to +break out of or continue, rather than affecting the innermost +loop by default. -#### Labels in for loop using `break`: +#### Labels in `for` loop using `break`: -The below code demonstrates the usage of a labels in `for` loop with `break`: +The following code demonstrates the usage of a label called `outerLoop` +in a `for` loop with a `break` statement: ```dart void main() { @@ -174,28 +180,26 @@ void main() { } print("outerLoop exited"); } - ``` -In the above example, when `i == 2` and `j == 2`, `break outerLoop;` statement stops both inner and outer loops. So, the expected output would be: +In the previous example, when `i == 2` and `j == 2`, the `break outerLoop;` +statement stops both inner and outer loops. So, the expected output is: ```dart - i = 1, j = 1 i = 1, j = 2 i = 1, j = 3 i = 2, j = 1 i = 2, j = 2 outerLoop exited - ``` -#### Labels in for loop using `continue`: +#### Labels in `for` loop using `continue`: -The below code demonstrates the usage of labels in `for` loop with `continue`: +The following code demonstrates the use of a label called `outerLoop` +in a `for` loop with a `continue` statement: ```dart - void main() { outerLoop: for (int i = 1; i <= 3; i++) { @@ -207,12 +211,12 @@ void main() { } } } - ``` -In the above example, when `i == 2` and `j == 2`, `continue outerLoop;` skips the rest of the iterations for `i = 2` and moves to `i = 3`. So, the output would be: -```dart +In the previous example, when `i == 2` and `j == 2`, `continue outerLoop;` skips the +rest of the iterations for `i = 2` and moves to `i = 3`. So, the output is: +```dart i = 1, j = 1 i = 1, j = 2 i = 1, j = 3 @@ -220,15 +224,14 @@ i = 2, j = 1 i = 3, j = 1 i = 3, j = 2 i = 3, j = 3 - ``` #### Labels in while loop using `break`: -The below code demonstrates the usage of a labels in `while` loop with `break`: +The following code demonstrates the use of a label called `outerLoop` in +a `while` loop with a `break` statement: ```dart - void main() { int i = 1; outerLoop: @@ -245,28 +248,26 @@ void main() { } print("outerLoop exited"); } - ``` -In the above example, the program breaks out of both inner and outer while loops when `i == 2` and `j == 2`.So, the expected output would be: +In the previous example, the program breaks out of both inner and outer `while` loops +when `i == 2` and `j == 2`.So, the expected output is: ```dart - i = 1, j = 1 i = 1, j = 2 i = 1, j = 3 i = 2, j = 1 i = 2, j = 2 outerLoop exited - ``` #### Labels in while loop using `continue`: -The below code demonstrates the usage of labels in `while` loop using `continue`: +The following code demonstrates the use of a label called `outerLoop` in +a `while` loop with a `continue` statement: ```dart - void main() { int i = 1; @@ -284,12 +285,12 @@ void main() { i++; } } - ``` -In the above example, the iteration for `i = 2` and `j = 2` is skipped, and the loop moves directly to `i = 3`. As a result, the output would be: -```dart +In the previous example, the iteration for `i = 2` and `j = 2` is skipped and the loop moves +directly to `i = 3`. As a result, the output is: +```dart i = 1, j = 1 i = 1, j = 2 i = 1, j = 3 @@ -297,14 +298,14 @@ i = 2, j = 1 i = 3, j = 1 i = 3, j = 2 i = 3, j = 3 - ``` #### Label in do while loop using `break`: -The below code demonstrate the usage of label in `do while` loop: -```dart +The following code demonstrates the use of a label called `outerLoop` in +a `do while` loop with a `break` statement: +```dart void main() { int i = 1; outerLoop: @@ -322,24 +323,24 @@ void main() { print("outerLoop exited"); } - ``` -In the above example, the program breaks out of both inner and outer loops when `i == 2` and `j == 2`. So, the expected output would be: -```dart +In the previous example, the program breaks out of both inner and outer loops when `i == 2` and +`j == 2`. So, the expected output is: +```dart i = 1, j = 1 i = 1, j = 2 i = 1, j = 3 i = 2, j = 1 i = 2, j = 2 outerLoop exited - ``` #### Labels in do while loop using `continue`: -The below code demonstrates the usage of labels in `do while` loop using `continue`: +The following code demonstrates the use of a label called `outerLoop` in +a `do while` loop with a `continue` statement: ```dart void main() { @@ -359,13 +360,12 @@ void main() { i++; } while (i <= 3); } - ``` -In the above example, the loop skips `i = 2` and `j = 2` and moves directly to `i = 3`. As a result, the output would be: +In the previous example, the loop skips `i = 2` and `j = 2` and moves directly to `i = 3`. +As a result, the output is: ```dart - i = 1, j = 1 i = 1, j = 2 i = 1, j = 3 @@ -373,7 +373,6 @@ i = 2, j = 1 i = 3, j = 1 i = 3, j = 2 i = 3, j = 3 - ``` [exceptions]: /language/error-handling From cff6fd2d36445d08a2035af479f0ef54a04a5fef Mon Sep 17 00:00:00 2001 From: Manoj J Date: Wed, 5 Mar 2025 22:41:17 +0530 Subject: [PATCH 16/20] Updated loops.md --- src/content/language/loops.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/content/language/loops.md b/src/content/language/loops.md index 805b6ff9cf..43701d5477 100644 --- a/src/content/language/loops.md +++ b/src/content/language/loops.md @@ -167,6 +167,7 @@ loop by default. The following code demonstrates the usage of a label called `outerLoop` in a `for` loop with a `break` statement: + ```dart void main() { outerLoop: @@ -199,6 +200,7 @@ outerLoop exited The following code demonstrates the use of a label called `outerLoop` in a `for` loop with a `continue` statement: + ```dart void main() { outerLoop: @@ -231,6 +233,7 @@ i = 3, j = 3 The following code demonstrates the use of a label called `outerLoop` in a `while` loop with a `break` statement: + ```dart void main() { int i = 1; @@ -267,10 +270,10 @@ outerLoop exited The following code demonstrates the use of a label called `outerLoop` in a `while` loop with a `continue` statement: + ```dart void main() { int i = 1; - outerLoop: while (i <= 3) { int j = 1; @@ -305,6 +308,7 @@ i = 3, j = 3 The following code demonstrates the use of a label called `outerLoop` in a `do while` loop with a `break` statement: + ```dart void main() { int i = 1; @@ -342,6 +346,7 @@ outerLoop exited The following code demonstrates the use of a label called `outerLoop` in a `do while` loop with a `continue` statement: + ```dart void main() { int i = 1; From f18047dbc40f31af14c75f97ab102bb40c03d498 Mon Sep 17 00:00:00 2001 From: Manoj J Date: Wed, 5 Mar 2025 22:51:12 +0530 Subject: [PATCH 17/20] Updated loops.md --- src/content/language/loops.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/language/loops.md b/src/content/language/loops.md index 43701d5477..50f947bcf1 100644 --- a/src/content/language/loops.md +++ b/src/content/language/loops.md @@ -270,7 +270,7 @@ outerLoop exited The following code demonstrates the use of a label called `outerLoop` in a `while` loop with a `continue` statement: - + ```dart void main() { int i = 1; From c5d961f25c6cdc654128a33558d63e7bb1066b03 Mon Sep 17 00:00:00 2001 From: Amanda Fitch <18406675+antfitch@users.noreply.github.com> Date: Wed, 5 Mar 2025 13:55:32 -0800 Subject: [PATCH 18/20] Update loops.md --- src/content/language/loops.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/content/language/loops.md b/src/content/language/loops.md index 50f947bcf1..8e559e43bc 100644 --- a/src/content/language/loops.md +++ b/src/content/language/loops.md @@ -228,7 +228,7 @@ i = 3, j = 2 i = 3, j = 3 ``` -#### Labels in while loop using `break`: +#### Labels in `while` loop using `break`: The following code demonstrates the use of a label called `outerLoop` in a `while` loop with a `break` statement: @@ -265,7 +265,7 @@ i = 2, j = 2 outerLoop exited ``` -#### Labels in while loop using `continue`: +#### Labels in `while` loop using `continue`: The following code demonstrates the use of a label called `outerLoop` in a `while` loop with a `continue` statement: @@ -303,7 +303,7 @@ i = 3, j = 2 i = 3, j = 3 ``` -#### Label in do while loop using `break`: +#### Labels in do `while` loop using `break`: The following code demonstrates the use of a label called `outerLoop` in a `do while` loop with a `break` statement: @@ -341,7 +341,7 @@ i = 2, j = 2 outerLoop exited ``` -#### Labels in do while loop using `continue`: +#### Labels in `do while` loop using `continue`: The following code demonstrates the use of a label called `outerLoop` in a `do while` loop with a `continue` statement: From e9c07e1f090c7a3501c9b8573aef4d1183b5e2d4 Mon Sep 17 00:00:00 2001 From: Manoj J Date: Sat, 8 Mar 2025 13:28:51 +0530 Subject: [PATCH 19/20] Update loops.md --- src/content/language/loops.md | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/content/language/loops.md b/src/content/language/loops.md index 8e559e43bc..51b3d75471 100644 --- a/src/content/language/loops.md +++ b/src/content/language/loops.md @@ -169,7 +169,7 @@ in a `for` loop with a `break` statement: ```dart -void main() { + outerLoop: for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { @@ -180,7 +180,7 @@ void main() { } } print("outerLoop exited"); -} + ``` In the previous example, when `i == 2` and `j == 2`, the `break outerLoop;` @@ -202,7 +202,7 @@ in a `for` loop with a `continue` statement: ```dart -void main() { + outerLoop: for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { @@ -212,7 +212,7 @@ void main() { print("i = $i, j = $j"); } } -} + ``` In the previous example, when `i == 2` and `j == 2`, `continue outerLoop;` skips the @@ -235,7 +235,7 @@ a `while` loop with a `break` statement: ```dart -void main() { + int i = 1; outerLoop: while (i <= 3) { @@ -250,7 +250,7 @@ void main() { i++; } print("outerLoop exited"); -} + ``` In the previous example, the program breaks out of both inner and outer `while` loops @@ -272,7 +272,7 @@ a `while` loop with a `continue` statement: ```dart -void main() { + int i = 1; outerLoop: while (i <= 3) { @@ -287,7 +287,7 @@ void main() { } i++; } -} + ``` In the previous example, the iteration for `i = 2` and `j = 2` is skipped and the loop moves @@ -310,7 +310,7 @@ a `do while` loop with a `break` statement: ```dart -void main() { + int i = 1; outerLoop: do { @@ -326,7 +326,7 @@ void main() { } while (i <= 3); print("outerLoop exited"); -} + ``` In the previous example, the program breaks out of both inner and outer loops when `i == 2` and @@ -348,9 +348,8 @@ a `do while` loop with a `continue` statement: ```dart -void main() { - int i = 1; + int i = 1; outerLoop: do { int j = 1; @@ -364,7 +363,7 @@ void main() { } while (j <= 3); i++; } while (i <= 3); -} + ``` In the previous example, the loop skips `i = 2` and `j = 2` and moves directly to `i = 3`. From c5feec540c2afe22da2d97fd771f7d1861f7668c Mon Sep 17 00:00:00 2001 From: Manoj J Date: Sat, 8 Mar 2025 13:31:32 +0530 Subject: [PATCH 20/20] Update loops.dart --- examples/language/lib/control_flow/loops.dart | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/examples/language/lib/control_flow/loops.dart b/examples/language/lib/control_flow/loops.dart index dd4d1ccc0f..df8c07ca6f 100644 --- a/examples/language/lib/control_flow/loops.dart +++ b/examples/language/lib/control_flow/loops.dart @@ -77,7 +77,6 @@ void miscDeclAnalyzedButNotTested() { { // #docregion label-for-loop-break - void main() { outerLoop: for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { @@ -88,13 +87,11 @@ void miscDeclAnalyzedButNotTested() { } } print("outerLoop exited"); - } // #enddocregion label-for-loop-break } { // #docregion label-for-loop-continue - void main() { outerLoop: for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { @@ -104,13 +101,11 @@ void miscDeclAnalyzedButNotTested() { print("i = $i, j = $j"); } } - } // #enddocregion label-for-loop-continue } { // #docregion label-while-loop-break - void main() { int i = 1; outerLoop: while (i <= 3) { @@ -125,13 +120,11 @@ void miscDeclAnalyzedButNotTested() { i++; } print("outerLoop exited"); - } // #enddocregion label-while-loop-break } { // #docregion label-while-loop-continue - void main() { int i = 1; outerLoop: @@ -147,14 +140,11 @@ void miscDeclAnalyzedButNotTested() { } i++; } - } // #enddocregion label-while-loop-continue } { // #docregion label-do-while-loop-break - - void main() { int i = 1; outerLoop: do { @@ -170,15 +160,12 @@ void miscDeclAnalyzedButNotTested() { } while (i <= 3); print("outerLoop exited"); - } // #enddocregion label-do-while-loop-break } { // #docregion label-do-while-loop-continue - void main() { int i = 1; - outerLoop: do { int j = 1; @@ -192,7 +179,6 @@ void miscDeclAnalyzedButNotTested() { } while (j <= 3); i++; } while (i <= 3); - } // #enddocregion label-do-while-loop-continue }