Lesson 5: Loops
While loops
While loops will repeatedly continue to execute the code in their code blocks until their given condition is met. This could be until a variable receives a boolean false value or until one variable is greater than another.
Example
Here is a simple example of while loop
let i = 1;
while (i <= 4) {
console.log(i);
i++;
}
This will output all the numbers from 1 to 4 in the console. Each time the loop runs the code, it increments the value of i by one. Eventually i will become larger in value than 4 and will causes the condition to become a falsy. After the condition has become a falsy, the while statement will stop looping through the code.
Try It!
Nesting loops
You can also use while loop inside another while loop, this is called **nested while loop**.
var i = 0;
while (i < 10) {
while (i < 5) {
i++;
}
i++;
console.log(i);
}
Using Variable in while loop
In while loop, the variable can be used to store the value of a condition.
The count variable in the above example can be used in the loop body to get desired output like executing equations.
Let's see an example to find the sum of even numbers from 1 to 10.
var count = 10, sum = 0;
while (count) {
sum = sum + count;
count = count - 2;
}
console.log(sum);
Infinite loops
In coding we need to be careful we don't create infinite loops. For example, if we had changed the condition above to i < 0 this would have been an infinite loop as i would never have reached a value less than 0. This is a common mistake in coding, and we should be careful not to create an infinite loop.
let i = 1;
while (i > 0) {
console.log(i);
i++;
}
Do/While loops
Do while loops run on the same principle of normal while loops. We use do while loops when we want the code in the loop to run at least once.
Let number = 3;
Let x = 0;
Do {
x += number;
console.log(x);
} while (x <= 100);
In the example,the statement will list all the multiples of 3 below the value of 100. If we switch the value of number to 60, it will list out the single multiple under 100 which would be just 60.
For loops
Try It!
Switch over to the index.js file
In a loop, the break keyword can be used to immediately break from the loop. When the system registers the break keyword it immediately stops iterating through the code in the loop block.
Can you guess at what number this loop will stop printing to the console?
for (let i = 0; i <= 10; i++) {
console.log(i);
}
Answer: The last number to be printed is 25 because when the iterator variable i reaches 26, the condition in the if statement is a truthy and the break keyword will cause the for loop to stop.
Differences between for and while loop in javascript
There are the following differences between for and while loop in javascript:
For loop | While loop |
---|---|
For loop runs on number of iterations given | While loop runs until the condition is false |
In for loop variable is incremented after the loop body is executed | In while loop variable can be updated anywhere at the start, middle, or end body execution |
A simple for loop can't be used when you don't know the number of iteration (but can be used by using break and if-else statement) | While loop can be used when you don't know the number of iteration and just know the condition to stop the loop |
Further Reading