Skip to content
Open
Changes from all commits
Commits
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
20 changes: 10 additions & 10 deletions 1-js/02-first-steps/13-while-for/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ Please note that the two `for` semicolons `;` must be present. Otherwise, there

Normally, a loop exits when its condition becomes falsy.

But we can force the exit at any time using the special `break` directive.
But we can force the exit at any time using the special `break` statement.

For example, the loop below asks the user for a series of numbers, "breaking" when no number is entered:

Expand All @@ -240,13 +240,13 @@ while (true) {
alert( 'Sum: ' + sum );
```

The `break` directive is activated at the line `(*)` if the user enters an empty line or cancels the input. It stops the loop immediately, passing control to the first line after the loop. Namely, `alert`.
The `break` statement is activated at the line `(*)` if the user enters an empty line or cancels the input. It stops the loop immediately, passing control to the first line after the loop. Namely, `alert`.

The combination "infinite loop + `break` as needed" is great for situations when a loop's condition must be checked not in the beginning or end of the loop, but in the middle or even in several places of its body.

## Continue to the next iteration [#continue]

The `continue` directive is a "lighter version" of `break`. It doesn't stop the whole loop. Instead, it stops the current iteration and forces the loop to start a new one (if the condition allows).
The `continue` statement is a "lighter version" of `break`. It doesn't stop the whole loop. Instead, it stops the current iteration and forces the loop to start a new one (if the condition allows).

We can use it if we're done with the current iteration and would like to move on to the next one.

Expand All @@ -262,9 +262,9 @@ for (let i = 0; i < 10; i++) {
}
```

For even values of `i`, the `continue` directive stops executing the body and passes control to the next iteration of `for` (with the next number). So the `alert` is only called for odd values.
For even values of `i`, the `continue` statement stops executing the body and passes control to the next iteration of `for` (with the next number). So the `alert` is only called for odd values.

````smart header="The `continue` directive helps decrease nesting"
````smart header="The `continue` statement helps decrease nesting"
A loop that shows odd values could look like this:

```js run
Expand All @@ -283,7 +283,7 @@ But as a side effect, this created one more level of nesting (the `alert` call i
````

````warn header="No `break/continue` to the right side of '?'"
Please note that syntax constructs that are not expressions cannot be used with the ternary operator `?`. In particular, directives such as `break/continue` aren't allowed there.
Please note that syntax constructs that are not expressions cannot be used with the ternary operator `?`. In particular, statements such as `break/continue` aren't allowed there.

For example, if we take this code:

Expand Down Expand Up @@ -368,7 +368,7 @@ outer:
for (let i = 0; i < 3; i++) { ... }
```

The `continue` directive can also be used with a label. In this case, code execution jumps to the next iteration of the labeled loop.
The `continue` statement can also be used with a label. In this case, code execution jumps to the next iteration of the labeled loop.

````warn header="Labels do not allow to \"jump\" anywhere"
Labels do not allow us to jump into an arbitrary place in the code.
Expand All @@ -381,7 +381,7 @@ break label; // jump to the label below (doesn't work)
label: for (...)
```

A `break` directive must be inside a code block. Technically, any labelled code block will do, e.g.:
A `break` statement must be inside a code block. Technically, any labelled code block will do, e.g.:

```js
label: {
Expand All @@ -404,8 +404,8 @@ We covered 3 types of loops:
- `do..while` -- The condition is checked after each iteration.
- `for (;;)` -- The condition is checked before each iteration, additional settings available.

To make an "infinite" loop, usually the `while(true)` construct is used. Such a loop, just like any other, can be stopped with the `break` directive.
To make an "infinite" loop, usually the `while(true)` construct is used. Such a loop, just like any other, can be stopped with the `break` statement.

If we don't want to do anything in the current iteration and would like to forward to the next one, we can use the `continue` directive.
If we don't want to do anything in the current iteration and would like to forward to the next one, we can use the `continue` statement.

`break/continue` support labels before the loop. A label is the only way for `break/continue` to escape a nested loop to go to an outer one.