URL: http://github.com/javascript-tutorial/uk.javascript.info/pull/78.diff
ти покрокове виконання цього прикладу на аркуші паперу. -Here's exactly what happens in our case: +Ось що відбувається в нашому випадку: ```js // for (let i = 0; i < 3; i++) alert(i) -// run begin +// Початок виконання let i = 0 -// if condition → run body and run step +// Якщо умова == true → виконати тіло і виконати крок if (i < 3) { alert(i); i++ } -// if condition → run body and run step +// Якщо умова == true → виконати тіло і виконати крок if (i < 3) { alert(i); i++ } -// if condition → run body and run step +// Якщо умова == true → виконати тіло і виконати крок if (i < 3) { alert(i); i++ } -// ...finish, because now i == 3 +// ...кінець, тому що зараз i == 3 ``` -````smart header="Inline variable declaration" -Here, the "counter" variable `i` is declared right in the loop. This is called an "inline" variable declaration. Such variables are visible only inside the loop. +````smart header="Вбудоване оголошення змінної" +В цьому прикладі всередині циклу оголошена змінна `i`, яка виконує функцію лічильника. Це так зване «вбудоване» оголошення змінної. Такі змінні доступні лише всередині циклу. ```js run for (*!*let*/!* i = 0; i < 3; i++) { alert(i); // 0, 1, 2 } -alert(i); // error, no such variable +alert(i); // помилка, немає такої змінної ``` -Instead of defining a variable, we could use an existing one: +Замість оголошення нової змінної, ми можемо використовувати існуючу: ```js run let i = 0; -for (i = 0; i < 3; i++) { // use an existing variable +for (i = 0; i < 3; i++) { // використовуємо існуючу змінну alert(i); // 0, 1, 2 } -alert(i); // 3, visible, because declared outside of the loop +alert(i); // 3, змінна доступна, тому що вона оголошена поза циклом ``` ```` -### Skipping parts +### Пропуск частин в "for" -Any part of `for` can be skipped. +Будь-яку частину `for` можна пропустити. -For example, we can omit `begin` if we don't need to do anything at the loop start. +Наприклад, ми можемо опустити `початок`, якщо нам не потрібно нічого робити перед стартом циклу. -Like here: +Ось так: ```js run -let i = 0; // we have i already declared and assigned +let i = 0; // ми вже маємо оголошену змінну і присвоєне значення -for (; i < 3; i++) { // no need for "begin" +for (; i < 3; i++) { // немає необхідності в "початку" alert( i ); // 0, 1, 2 } ``` -We can also remove the `step` part: +Ми також можемо видалити частину `крок`: ```js run let i = 0; @@ -192,32 +192,32 @@ for (; i < 3;) { } ``` -This makes the loop identical to `while (i < 3)`. +Це робить цикл ідентичним до `while (i < 3)`. -We can actually remove everything, creating an infinite loop: +Можна взагалі забрати все, отримавши нескінченний цикл: ```js for (;;) { - // repeats without limits + // буде вічно повторюватися } ``` -Please note that the two `for` semicolons `;` must be present. Otherwise, there would be a syntax error. +Зауважте, що ці двокрапки `;` повинні бути, інакше виникне синтаксична помилка. -## Breaking the loop +## Переривання циклу: "break" -Normally, a loop exits when its condition becomes falsy. +Зазвичай, цикл завершується, коли умова стає `false`. -But we can force the exit at any time using the special `break` directive. +Але ми можемо в будь-який момент вийти з циклу, використавши спеціальну директиву `break`. -For example, the loop below asks the user for a series of numbers, "breaking" when no number is entered: +Наприклад, наступний код запитує в користувача число до тих пір, поки користувач їх вводить. Після того, як користувач не ввів число — цикл завершується (директивою "break") і виводить суму чисел: ```js run let sum = 0; while (true) { - let value = +prompt("Enter a number", ''); + let value = +prompt("Введіть число", ''); *!* if (!value) break; // (*) @@ -226,35 +226,35 @@ while (true) { sum += value; } -alert( 'Sum: ' + sum ); +alert( 'Сума: ' + 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`. +Директива `break` в рядку `(*)` спрацьовує тоді, коли користувач вводить порожній рядок або скасовує введення. Ця директива негайно завершує виконання циклу і передає контроль наступному рядку за циклом, тобто на `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. +Комбінація «нескінченний цикл + `break`» — чудова річ для тих ситуацій, коли умова для переривання знаходиться не на початку або кінці циклу, а всередині (або навіть в декількох місцях) тіла циклу. -## Continue to the next iteration [#continue] +## Продовження з наступної ітерації [#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). +Директива `continue` — це "полегшена версія" `break`. Вона не зупиняє весь цикл. Натомість, вона зупиняє поточну ітерацію і починає виконання циклу спочатку з наступної ітерації (якщо умова циклу досі вірна). -We can use it if we're done with the current iteration and would like to move on to the next one. +Її зручно використовувати коли закінчили з поточною ітерацією і хочемо продовжити з наступної. -The loop below uses `continue` to output only odd values: +Цикл нижче використовує `continue` щоб вивести лише непарні значення: ```js run no-beautify for (let i = 0; i < 10; i++) { - // if true, skip the remaining part of the body + // якщо умова справджується, тоді пропускаємо решту тіла циклу і починаємо з наступної ітерації *!*if (i % 2 == 0) continue;*/!* - alert(i); // 1, then 3, 5, 7, 9 + alert(i); // 1, потім 3, 5, 7, 9 } ``` -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. +Для парних значень змінної `i`, директива `continue` зупиняє виконання тіла циклу і передає контроль наступній ітерації в `for` (в цьому випадку це буде наступне число). Таким чином функція `alert` викликається лише для непарних значень змінної `i`. -````smart header="The `continue` directive helps decrease nesting" -A loop that shows odd values could look like this: +````smart header="Директива `continue` допомагає зменшити рівень вкладеності" +Цикл, який показує непарні значення може виглядати так: ```js run for (let i = 0; i < 10; i++) { @@ -266,15 +266,15 @@ for (let i = 0; i < 10; i++) { } ``` -From a technical point of view, this is identical to the example above. Surely, we can just wrap the code in an `if` block instead of using `continue`. +З технічної точки зору, цей приклад ідентичний тому що вище. Звичайно, ми можемо просто обгорнути код в блок `if` замість використання `continue`. -But as a side-effect, this created one more level of nesting (the `alert` call inside the curly braces). If the code inside of `if` is longer than a few lines, that may decrease the overall readability. +Але побічним ефектом цього буде створення ще одного рівня вкладеності (виклик `alert` всередині фігурних дужок). Якщо код всередині `if` буде більшим за декілька рядків, то це може ускладнити загальну читабельність коду. ```` -````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. +````warn header="Директиви `break/continue` праворуч від '?' не працюють" +Майте на увазі, що такі синтаксичні конструкції, які не є виразами, не можуть використовуватися з тернарним оператором `?`. Власне, такі директиви як `break/continue` там не дозволені. -For example, if we take this code: +Наприклад, якщо взяти код: ```js if (i > 5) { @@ -284,103 +284,103 @@ if (i > 5) { } ``` -...and rewrite it using a question mark: +...і переробити його з використанням знака питання: ```js no-beautify -(i > 5) ? alert(i) : *!*continue*/!*; // continue isn't allowed here +(i > 5) ? alert(i) : *!*continue*/!*; // використання continue в недозволеному місці ``` -...it stops working: there's a syntax error. +...то такий код перестане працювати: виникне синтаксична помилка. -This is just another reason not to use the question mark operator `?` instead of `if`. +Це ще одна причина не використовувати для умов оператор знака питання `?`, замість повноцінного `if`. ```` -## Labels for break/continue +## Мітки для break/continue -Sometimes we need to break out from multiple nested loops at once. +Деколи нам потрібно вийти з кількох вкладених циклів. -For example, in the code below we loop over `i` and `j`, prompting for the coordinates `(i, j)` from `(0,0)` to `(2,2)`: +Наприклад, в коді нижче є 2 цикли, які проходяться по змінних `i` та `j`, і запитують в користувача координати `(i, j)` від `(0,0)` до `(2,2)`: ```js run no-beautify for (let i = 0; i < 3; i++) { for (let j = 0; j < 3; j++) { - let input = prompt(`Value at coords (${i},${j})`, ''); + let input = prompt(`Значення в координатах (${i},${j})`, ''); - // what if we want to exit from here to Done (below)? + // що якщо ми хочемо вийти звідси відразу до 'Готово!' (в функцію alert нижче)? } } -alert('Done!'); +alert('Готово!'); ``` -We need a way to stop the process if the user cancels the input. +Нам потрібен спосіб зупинити ці два цикли, якщо користувач скасує введення. -The ordinary `break` after `input` would only break the inner loop. That's not sufficient--labels, come to the rescue! +Звичайний `break` після `input` лише перерве внутрішній цикл, а нам цього недостатньо. Ось тут нам пригодяться мітки для циклів! -A *label* is an identifier with a colon before a loop: +*Мітка* складається з ідентифікатора та двокрапки перед циклом: ```js labelName: for (...) { ... } ``` -The `breakNote: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: