while loop
JavaScript have the while loop to execute the block of code repeatedly till the condition is true. Once the condition is false than the loop will be terminated.
While loop have only one argument that is condition expression.
Syntax:
while (condition) { // Block of code // Code to execute }
Example:
var i = 0; while (i < 10) { console.log(++i); } // 1 2 3 4 5 6 7 8 9 10
Above example will print the 1 to 10 in the browser’s console window. In above while the condition is i less than 10, so while loop every time check the condition than the value of the i is less than 10 or not. If i less than 10 than execute the block of code other wise not.
Execution flow of While loop
Step 1: While loop first check the condition,
Step 2: If condition is true than execute code and go to Step 1. If condition is false than loop will be terminated.
While loop is helpful when we does not know exact how much time loop needed to be executed. It is helpful when we have to read some stream like read the file or read the content from the server.
Do-While loop
JavaScript also provides the do-while loop, that helpful us to execute block of code repeatedly till the condition is true.
It different from the while is that, while loop check the condition first and execute the code after if the condition is true, whereas the do-while loop execute code first and check the condition after it. It means do-while loop will be executed minimum one time even the condition is false.
Yes, the do-while loop will be executed minimum one time even the condition is false, So please take care of it.
Syntax:
do { // block of code to execute } while (condition);
Example:
var i = 0; do { console.log(++i); } while (i < 10); // 1 2 3 4 5 6 7 8 9 10
Above example also print the 1 to 10 in the browser’s console.
Do-While loop when condition is false
The do-while loop will be executed minimum one time even the condition is false, So please take care of it.
Example:
var i = 0; do { console.log(++i); } while (i < -1); // 1
In above example the condition is false but the do-while loop check the condition in last and executes the code first, so that’s why the it executes minimum one time
Execution flow of do-while loop
Step 1: Execute block of code first
Step 2: Check the condition
Step 3: It condition is true go to the step 1, If the condition is false the terminate the loop
While vs Do-While loop
Do-While loop at least execute one time because it check the condition at the end, whereas the While loop check the condition first.
Infinite While Loop
var i = 0; while (true) { console.log(++i); if (i >= 10) { break; } }
Above loop is infinite loop, we just put the true in the while loop, so the condition is always right, so loop will be infinite, So we have to use the break statement to terminate the loop. Otherwise the browser will be hanged.
While Loop on Array
var numbers = [1, 2, 3, 4, 5]; var i = 0; while (numbers[i]) { console.log(numbers[i++]); }
In above example we create the loop in the array, Above example will be print the 1 to 5 on the browser’s console. Here while loop checking the condition by the checking the value in array. If while loop find the index in the array the block of code is executed, if the index not found in the array than loop will be terminated.
Value for the while loop to terminate the loop
- Passing by 0 (ZERO) to while loop
- Passing by false to while loop
- Passing null or undefined value to the loop.