en.javascript.info/1-js/02-first-steps/12-while-for/3-which-value-for/solution.md
2017-09-25 20:29:22 +03:00

17 lines
549 B
Markdown

**The answer: from `0` to `4` in both cases.**
```js run
for (let i = 0; i < 5; ++i) alert( i );
for (let i = 0; i < 5; i++) alert( i );
```
That can be easily deducted from the algorithm of `for`:
1. Execute once `i = 0` before everything (begin).
2. Check the condition `i < 5`
3. If `true` -- execute the loop body `alert(i)`, and then `i++`
The increment `i++` is separated from the condition check (2). That's just another statement.
The value returned by the increment is not used here, so there's no difference between `i++` and `++i`.