Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explaining the concepts of Label (break and continue) in Dart #6451

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions examples/language/lib/control_flow/loops.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,112 @@ void miscDeclAnalyzedButNotTested() {
.forEach((c) => c.interview());
// #enddocregion where
}

{
// #docregion label-for-loop-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("outerLoop exited");
// #enddocregion label-for-loop-break
}

{
// #docregion label-for-loop-continue
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
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
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
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
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
}

}
237 changes: 237 additions & 0 deletions src/content/language/loops.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,243 @@ candidates
.forEach((c) => c.interview());
```

### Labels

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:

* `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.

* `continue labelName;`: Skips the rest of the current iteration of the
labeled statement loop and continues with the next iteration.

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`:

The following code demonstrates the usage of a label called `outerLoop`
in a `for` loop with a `break` statement:

<?code-excerpt "language/lib/control_flow/loops.dart (label-for-loop-break)"?>
```dart

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 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`:

The following code demonstrates the use of a label called `outerLoop`
in a `for` loop with a `continue` statement:

<?code-excerpt "language/lib/control_flow/loops.dart (label-for-loop-continue)"?>
```dart

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 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
i = 2, j = 1
i = 3, j = 1
i = 3, j = 2
i = 3, j = 3
```

#### 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:

<?code-excerpt "language/lib/control_flow/loops.dart (label-while-loop-break)"?>
```dart

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 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 following code demonstrates the use of a label called `outerLoop` in
a `while` loop with a `continue` statement:

<?code-excerpt "language/lib/control_flow/loops.dart (label-while-loop-continue)"?>
```dart

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 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
i = 2, j = 1
i = 3, j = 1
i = 3, j = 2
i = 3, j = 3
```

#### 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:

<?code-excerpt "language/lib/control_flow/loops.dart (label-do-while-loop-break)"?>
```dart

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 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 following code demonstrates the use of a label called `outerLoop` in
a `do while` loop with a `continue` statement:

<?code-excerpt "language/lib/control_flow/loops.dart (label-do-while-loop-continue)"?>
```dart

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 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
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
Expand Down