@@ -17,7 +17,7 @@ while (condition) {
17
17
}
18
18
```
19
19
20
- While the ` condition ` is ` true ` , the ` code ` from the loop body is executed.
20
+ While the ` condition ` is truthy , the ` code ` from the loop body is executed.
21
21
22
22
For instance, the loop below outputs ` i ` while ` i < 3 ` :
23
23
@@ -47,8 +47,8 @@ while (i) { // when i becomes 0, the condition becomes falsy, and the loop stops
47
47
}
48
48
```
49
49
50
- ```` smart header="Brackets are not required for a single-line body"
51
- If the loop body has a single statement, we can omit the brackets `{…}`:
50
+ ```` smart header="Curly braces are not required for a single-line body"
51
+ If the loop body has a single statement, we can omit the curly braces `{…}`:
52
52
53
53
```js run
54
54
let i = 3;
@@ -84,7 +84,7 @@ This form of syntax should only be used when you want the body of the loop to ex
84
84
85
85
## The "for" loop
86
86
87
- The ` for ` loop is the most commonly used loop.
87
+ The ` for ` loop is more complex, but it's also the most commonly used loop.
88
88
89
89
It looks like this:
90
90
@@ -111,8 +111,8 @@ Let's examine the `for` statement part-by-part:
111
111
| body | ` alert(i) ` | Runs again and again while the condition is truthy. |
112
112
| step| ` i++ ` | Executes after the body on each iteration. |
113
113
114
-
115
114
The general loop algorithm works like this:
115
+
116
116
```
117
117
Run begin
118
118
→ (if condition → run body and run step)
@@ -121,6 +121,8 @@ Run begin
121
121
→ ...
122
122
```
123
123
124
+ That is, ` begin ` executes once, and then it iterates: after each ` condition ` test, ` body ` and ` step ` are executed.
125
+
124
126
If you are new to loops, it could help to go back to the example and reproduce how it runs step-by-step on a piece of paper.
125
127
126
128
Here's exactly what happens in our case:
@@ -289,8 +291,7 @@ if (i > 5) {
289
291
(i > 5) ? alert(i) : *!*continue*/!*; // continue isn't allowed here
290
292
```
291
293
292
- ...it stops working. Code like this will give a syntax error:
293
-
294
+ ...it stops working: there's a syntax error.
294
295
295
296
This is just another reason not to use the question mark operator `?` instead of `if`.
296
297
````
@@ -357,12 +358,12 @@ for (let i = 0; i < 3; i++) { ... }
357
358
358
359
The ` continue ` directive can also be used with a label. In this case, code execution jumps to the next iteration of the labeled loop.
359
360
360
- ```` warn header="Labels are not a \" goto \" "
361
+ ```` warn header="Labels do not allow to \" jump \" anywhere "
361
362
Labels do not allow us to jump into an arbitrary place in the code.
362
363
363
364
For example, it is impossible to do this:
364
365
```js
365
- break label; // jumps to label? No.
366
+ break label; // doesn't jumps to the label below
366
367
367
368
label: for (...)
368
369
```
0 commit comments