JavaScript WHILE Loop (PDF)
Document Details
Uploaded by MagnanimousCloisonnism
Vilnius University
Justina Balsė
Tags
Summary
This document provides an introduction to JavaScript WHILE loops, explanations and example usage. Including Increment/Decrement and different types of WHILE loops.
Full Transcript
JavaScript pagrindai (WHILE) Justina Balsė Turinys Increment/Decrement; Nežinomo kartojimų skaičiaus ciklas WHILE; break; continue; 2 Increment and Decrement // Increment The increment and decrement let a = 1...
JavaScript pagrindai (WHILE) Justina Balsė Turinys Increment/Decrement; Nežinomo kartojimų skaičiaus ciklas WHILE; break; continue; 2 Increment and Decrement // Increment The increment and decrement let a = 1; operators in JavaScript will a++; add one (+1) or subtract one ++a; (-1) // Decrement The ++/-- operators can be let b = 1; used before or after the b--; operand --b; Justina Balsė 3 Using ++/-- After the Operand // Increment When you use the let a = 1; increment/decrement operator console.log(a++); // 1 after the operand, the value console.log(a); // 2 will be returned before the // Decrement operand is let b = 1; increased/decreased. console.log(b--); // 1 console.log(b); // 0 Justina Balsė 4 Using ++/-- Before the Operand // Increment If you’d rather make the let a = 1; variable increment/decrement console.log(++a); // 2 before returning, you simply console.log(a); // 2 have to use the // Decrement increment/decrement operator let b = 1; before the operand. console.log(--b); // 0 console.log(b); // 0 Justina Balsė 5 Loop | Ciklas We often need to repeat actions. For example, outputting goods from a list one after another or just running the same code for each number from 1 to 10. Loops are a way to repeat the same code multiple times. Justina Balsė 6 WHILE while (condition) { The while statement creates a // code loop that executes a specified // so-called "loop body" statement as long as the test } condition evaluates to true. The condition is evaluated before executing the statement. Justina Balsė 7 WHILE let i = 0; In while loop we have only while(i < 5) { space for checking condition. console.log(i); If we forgot the increment then // i++; it will result in infinite loop. i = i + 1; A single execution of the loop } body is called an iteration. // output The loop in the example makes five iterations. // 0 1 2 3 4 Justina Balsė 8 DO...WHILE let i = 0; The loop will first execute the do { body, then check the condition, alert( i ); and, while it’s truthy, execute it i++; again and again. } while (i < 3); Justina Balsė 9 WHILE vs. DO-WHILE Justina Balsė 10 BREAK | 1 let i = 0; The combination “infinite loop + while(i < 5) { break as needed” is great for if(i == 2) { situations when a loop’s break; condition must be checked not in } the beginning or end of the loop, console.log(i); but in the middle or even in i = i + 1; several places of its body. } //output // 0 1 Justina Balsė 11 ***BREAK | 2 let sum = 0; The combination “infinite loop + while (true) { break as needed” is great for let value = +prompt("Enter a number", ''); situations when a loop’s if (!value) break; condition must be checked not in sum += value; the beginning or end of the loop, } but in the middle or even in alert( 'Sum: ' + sum ); several places of its body. Justina Balsė 12 CONTINUE let i = 0; The continue directive is a while(i < 5) { “lighter version” of break. It if(i == 2) { doesn’t stop the whole loop. continue; } Instead, it stops the current console.log(i); iteration and forces the loop i = i + 1; to start a new one (if the } condition allows). //output // 0 1 … Justina Balsė 13 PHP example ?> Justina Balsė 14 Praktika Uždavinių sprendimas konsolėje. 03 - Cikliniai algoritmai WHILE (PraktikaEN) 03 - Cikliniai algoritmai WHILE (PraktikaLT) 15