Skip to content

Commit 9bb22df

Browse files
J-Manoj-06antfitchparlough
authored
Explaining the concepts of Label (break and continue) in Dart (#6451)
This PR improves the Dart documentation by adding detailed explanations and examples for the ```break``` and ```continue``` statements in various loop structures (for, while, do-while, and nested loops). The changes have made in two directories: 1) ```/examples/language/lib/control_flow/branches.dart``` 2) ```/src/content/language/branches.md``` Preview: https://dart-dev--pr6451-j-manoj-06-patch-4-fywn6bb1.web.app/language/loops#labels - [x] I’ve reviewed the contributor guide and applied the relevant portions to this PR. - [x] This PR doesn't contain automatically generated corrections or text (Grammarly, LLMs, and similar). - [x] This PR follows the [Google Developer Documentation Style Guidelines](https://developers.google.com/style) — for example, it doesn't use _i.e._ or _e.g._, and it avoids _I_ and _we_ (first person). - [x] This PR uses [semantic line breaks](https://github.com/dart-lang/site-shared/blob/main/doc/writing-for-dart-and-flutter-websites.md#semantic-line-breaks) of 80 characters or fewer. This ensures and follows the contributing guidelines. As per the issue , the Labels in Dart is explained with examples. Waiting for further improvements. Fixes #6289 Best regards, Manoj J --------- Co-authored-by: Amanda Fitch <18406675+antfitch@users.noreply.github.com> Co-authored-by: Parker Lougheed <parlough@gmail.com>
1 parent 27f0b42 commit 9bb22df

File tree

2 files changed

+341
-0
lines changed

2 files changed

+341
-0
lines changed

examples/language/lib/control_flow/loops.dart

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,114 @@ void miscDeclAnalyzedButNotTested() {
7474
.forEach((c) => c.interview());
7575
// #enddocregion where
7676
}
77+
78+
{
79+
// #docregion label-for-loop-break
80+
outerLoop:
81+
for (var i = 1; i <= 3; i++) {
82+
for (var j = 1; j <= 3; j++) {
83+
print('i = $i, j = $j');
84+
if (i == 2 && j == 2) {
85+
break outerLoop;
86+
}
87+
}
88+
}
89+
print('outerLoop exited');
90+
// #enddocregion label-for-loop-break
91+
}
92+
93+
{
94+
// #docregion label-for-loop-continue
95+
outerLoop:
96+
for (var i = 1; i <= 3; i++) {
97+
for (var j = 1; j <= 3; j++) {
98+
if (i == 2 && j == 2) {
99+
continue outerLoop;
100+
}
101+
print('i = $i, j = $j');
102+
}
103+
}
104+
// #enddocregion label-for-loop-continue
105+
}
106+
107+
{
108+
// #docregion label-while-loop-break
109+
var i = 1;
110+
111+
outerLoop:
112+
while (i <= 3) {
113+
var j = 1;
114+
while (j <= 3) {
115+
print('i = $i, j = $j');
116+
if (i == 2 && j == 2) {
117+
break outerLoop;
118+
}
119+
j++;
120+
}
121+
i++;
122+
}
123+
print('outerLoop exited');
124+
// #enddocregion label-while-loop-break
125+
}
126+
127+
{
128+
// #docregion label-while-loop-continue
129+
var i = 1;
130+
131+
outerLoop:
132+
while (i <= 3) {
133+
var j = 1;
134+
while (j <= 3) {
135+
if (i == 2 && j == 2) {
136+
i++;
137+
continue outerLoop;
138+
}
139+
print('i = $i, j = $j');
140+
j++;
141+
}
142+
i++;
143+
}
144+
// #enddocregion label-while-loop-continue
145+
}
146+
147+
{
148+
// #docregion label-do-while-loop-break
149+
var i = 1;
150+
151+
outerLoop:
152+
do {
153+
var j = 1;
154+
do {
155+
print('i = $i, j = $j');
156+
if (i == 2 && j == 2) {
157+
break outerLoop;
158+
}
159+
j++;
160+
} while (j <= 3);
161+
i++;
162+
} while (i <= 3);
163+
164+
print('outerLoop exited');
165+
// #enddocregion label-do-while-loop-break
166+
}
167+
168+
{
169+
// #docregion label-do-while-loop-continue
170+
var i = 1;
171+
172+
outerLoop:
173+
do {
174+
var j = 1;
175+
do {
176+
if (i == 2 && j == 2) {
177+
i++;
178+
continue outerLoop;
179+
}
180+
print('i = $i, j = $j');
181+
j++;
182+
} while (j <= 3);
183+
i++;
184+
} while (i <= 3);
185+
// #enddocregion label-do-while-loop-continue
186+
}
77187
}

src/content/language/loops.md

Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,237 @@ candidates
142142
.forEach((c) => c.interview());
143143
```
144144

145+
## Labels
146+
147+
A label is an identifier followed by a colon (`labelName:`)
148+
that you can place before a statement to create a
149+
_labeled statement_. Loops and switch cases are often used as
150+
labeled statements. A labeled statement can be referenced later
151+
in a `break` or `continue` statement as follows:
152+
153+
* `break labelName;`
154+
Terminates the execution of the labeled statement.
155+
This is useful for breaking out of a specific outer loop when you're
156+
within a nested loop.
157+
158+
* `continue labelName;`
159+
Skips the rest of the current iteration of the
160+
labeled statement loop and continues with the next iteration.
161+
162+
Labels are used to manage control flow. They are often used with
163+
loops and switch cases and allow you to specify which statement to
164+
break out of or continue, rather than affecting the innermost
165+
loop by default.
166+
167+
### Labels in for loop using `break` {:.no_toc}
168+
169+
The following code demonstrates the usage of a label called `outerLoop`
170+
in a `for` loop with a `break` statement:
171+
172+
<?code-excerpt "language/lib/control_flow/loops.dart (label-for-loop-break)"?>
173+
```dart
174+
outerLoop:
175+
for (var i = 1; i <= 3; i++) {
176+
for (var j = 1; j <= 3; j++) {
177+
print('i = $i, j = $j');
178+
if (i == 2 && j == 2) {
179+
break outerLoop;
180+
}
181+
}
182+
}
183+
print('outerLoop exited');
184+
```
185+
186+
In the previous example, when `i == 2` and `j == 2`, the `break outerLoop;`
187+
statement stops both inner and outer loops. As a result, the output is:
188+
189+
```plaintext
190+
i = 1, j = 1
191+
i = 1, j = 2
192+
i = 1, j = 3
193+
i = 2, j = 1
194+
i = 2, j = 2
195+
outerLoop exited
196+
```
197+
198+
### Labels in for loop using `continue` {:.no_toc}
199+
200+
The following code demonstrates the use of a label called `outerLoop`
201+
in a `for` loop with a `continue` statement:
202+
203+
<?code-excerpt "language/lib/control_flow/loops.dart (label-for-loop-continue)"?>
204+
```dart
205+
outerLoop:
206+
for (var i = 1; i <= 3; i++) {
207+
for (var j = 1; j <= 3; j++) {
208+
if (i == 2 && j == 2) {
209+
continue outerLoop;
210+
}
211+
print('i = $i, j = $j');
212+
}
213+
}
214+
```
215+
216+
In the previous example, when `i == 2` and `j == 2`, `continue outerLoop;` skips the
217+
rest of the iterations for `i = 2` and moves to `i = 3`. As a result, the output is:
218+
219+
```plaintext
220+
i = 1, j = 1
221+
i = 1, j = 2
222+
i = 1, j = 3
223+
i = 2, j = 1
224+
i = 3, j = 1
225+
i = 3, j = 2
226+
i = 3, j = 3
227+
```
228+
229+
### Labels in while loop using `break` {:.no_toc}
230+
231+
The following code demonstrates the use of a label called `outerLoop` in
232+
a `while` loop with a `break` statement:
233+
234+
<?code-excerpt "language/lib/control_flow/loops.dart (label-while-loop-break)"?>
235+
```dart
236+
var i = 1;
237+
238+
outerLoop:
239+
while (i <= 3) {
240+
var j = 1;
241+
while (j <= 3) {
242+
print('i = $i, j = $j');
243+
if (i == 2 && j == 2) {
244+
break outerLoop;
245+
}
246+
j++;
247+
}
248+
i++;
249+
}
250+
print('outerLoop exited');
251+
```
252+
253+
In the previous example, the program breaks out of both inner and outer `while` loops
254+
when `i == 2` and `j == 2`. As a result, the output is:
255+
256+
```plaintext
257+
i = 1, j = 1
258+
i = 1, j = 2
259+
i = 1, j = 3
260+
i = 2, j = 1
261+
i = 2, j = 2
262+
outerLoop exited
263+
```
264+
265+
### Labels in while loop using `continue` {:.no_toc}
266+
267+
The following code demonstrates the use of a label called `outerLoop` in
268+
a `while` loop with a `continue` statement:
269+
270+
<?code-excerpt "language/lib/control_flow/loops.dart (label-while-loop-continue)"?>
271+
```dart
272+
var i = 1;
273+
274+
outerLoop:
275+
while (i <= 3) {
276+
var j = 1;
277+
while (j <= 3) {
278+
if (i == 2 && j == 2) {
279+
i++;
280+
continue outerLoop;
281+
}
282+
print('i = $i, j = $j');
283+
j++;
284+
}
285+
i++;
286+
}
287+
```
288+
289+
In the previous example, the iteration for `i = 2` and `j = 2` is skipped and the loop moves
290+
directly to `i = 3`. As a result, the output is:
291+
292+
```plaintext
293+
i = 1, j = 1
294+
i = 1, j = 2
295+
i = 1, j = 3
296+
i = 2, j = 1
297+
i = 3, j = 1
298+
i = 3, j = 2
299+
i = 3, j = 3
300+
```
301+
302+
### Labels in do-while loop using `break` {:.no_toc}
303+
304+
The following code demonstrates the use of a label called `outerLoop` in
305+
a `do while` loop with a `break` statement:
306+
307+
<?code-excerpt "language/lib/control_flow/loops.dart (label-do-while-loop-break)"?>
308+
```dart
309+
var i = 1;
310+
311+
outerLoop:
312+
do {
313+
var j = 1;
314+
do {
315+
print('i = $i, j = $j');
316+
if (i == 2 && j == 2) {
317+
break outerLoop;
318+
}
319+
j++;
320+
} while (j <= 3);
321+
i++;
322+
} while (i <= 3);
323+
324+
print('outerLoop exited');
325+
```
326+
327+
In the previous example, the program breaks out of both inner and outer loops when `i == 2` and
328+
`j == 2`. As a result, the output is:
329+
330+
```plaintext
331+
i = 1, j = 1
332+
i = 1, j = 2
333+
i = 1, j = 3
334+
i = 2, j = 1
335+
i = 2, j = 2
336+
outerLoop exited
337+
```
338+
339+
### Labels in do-while loop using `continue` {:.no_toc}
340+
341+
The following code demonstrates the use of a label called `outerLoop` in
342+
a `do while` loop with a `continue` statement:
343+
344+
<?code-excerpt "language/lib/control_flow/loops.dart (label-do-while-loop-continue)"?>
345+
```dart
346+
var i = 1;
347+
348+
outerLoop:
349+
do {
350+
var j = 1;
351+
do {
352+
if (i == 2 && j == 2) {
353+
i++;
354+
continue outerLoop;
355+
}
356+
print('i = $i, j = $j');
357+
j++;
358+
} while (j <= 3);
359+
i++;
360+
} while (i <= 3);
361+
```
362+
363+
In the previous example, the loop skips `i = 2` and `j = 2` and moves directly to `i = 3`.
364+
As a result, the output is:
365+
366+
```plaintext
367+
i = 1, j = 1
368+
i = 1, j = 2
369+
i = 1, j = 3
370+
i = 2, j = 1
371+
i = 3, j = 1
372+
i = 3, j = 2
373+
i = 3, j = 3
374+
```
375+
145376
[exceptions]: /language/error-handling
146377
[branching]: /language/branches
147378
[iteration]: /libraries/dart-core#iteration

0 commit comments

Comments
 (0)