minor fixes
This commit is contained in:
parent
689975093c
commit
f6ff773033
24 changed files with 67 additions and 76 deletions
|
@ -17,7 +17,7 @@ while (condition) {
|
|||
}
|
||||
```
|
||||
|
||||
While the `condition` is `true`, the `code` from the loop body is executed.
|
||||
While the `condition` is truthy, the `code` from the loop body is executed.
|
||||
|
||||
For instance, the loop below outputs `i` while `i < 3`:
|
||||
|
||||
|
@ -84,7 +84,7 @@ This form of syntax should only be used when you want the body of the loop to ex
|
|||
|
||||
## The "for" loop
|
||||
|
||||
The `for` loop is the most commonly used loop.
|
||||
The `for` loop is more complex, but it's also the most commonly used loop.
|
||||
|
||||
It looks like this:
|
||||
|
||||
|
@ -111,8 +111,8 @@ Let's examine the `for` statement part-by-part:
|
|||
| step| `i++` | Executes after the body on each iteration but before the condition check. |
|
||||
| body | `alert(i)`| Runs again and again while the condition is truthy. |
|
||||
|
||||
|
||||
The general loop algorithm works like this:
|
||||
|
||||
```
|
||||
Run begin
|
||||
→ (if condition → run body and run step)
|
||||
|
@ -121,6 +121,8 @@ Run begin
|
|||
→ ...
|
||||
```
|
||||
|
||||
That is, `begin` executes once, and then it iterates: after each `condition` test, `body` and `step` are executed.
|
||||
|
||||
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.
|
||||
|
||||
Here's exactly what happens in our case:
|
||||
|
@ -289,8 +291,7 @@ if (i > 5) {
|
|||
(i > 5) ? alert(i) : *!*continue*/!*; // continue isn't allowed here
|
||||
```
|
||||
|
||||
...it stops working. Code like this will give a syntax error:
|
||||
|
||||
...it stops working: there's a syntax error.
|
||||
|
||||
This is just another reason not to use the question mark operator `?` instead of `if`.
|
||||
````
|
||||
|
@ -358,12 +359,12 @@ 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.
|
||||
|
||||
````warn header="Labels are not a \"goto\""
|
||||
````warn header="Labels do not allow to \"jump\" anywhere"
|
||||
Labels do not allow us to jump into an arbitrary place in the code.
|
||||
|
||||
For example, it is impossible to do this:
|
||||
```js
|
||||
break label; // jumps to label? No.
|
||||
break label; // doesn't jumps to the label below
|
||||
|
||||
label: for (...)
|
||||
```
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue