Before delving into this topic, you might have made yourself a cup of coffee to sip it as you read. In the meantime, the weather conditions outside might be changing; it may have started raining after a cloudy morning. As it rains, the pavements become wet, and puddles grow. All these scenarios, based on the while ... (condition) structure, illustrate the concept of loops.
A loop is a segment of code that repeats a specified number of times. While loops continue as long as a certain condition remains true, like while you are reading the topic or while it is raining. Some conditions are always true. For example, if you write while (5 < 6), this results in an infinite loop, as 5 is perennially less than 6. Conversely, other conditions lead to a finite number of loop iterations.
While syntax
The while loop takes the following structure:
while (condition) {
...
}
This loop runs as long as the condition == true. Once the stated condition turns false, the loop terminates.
Check out the example below:
let n = 2;
while (n < 100) {
n = n * n;
console.log(n);
}
Ultimately, we get this result:
4
16
256
How did this happen? Let's go through each iteration step by step.
In the beginning, the variable n = 2. We immediately check if n < 100 upon entering the while loop. This condition is true, which brings us into the loop. Next, we square n and output the result, which is why we get 4 on the first line.
In the second iteration, we check the condition before running the loop. 4 * 4 = 16: that's the second line. As 16 < 100 is true, we execute the loop for the third time, multiplying 16 by itself to get 256. However, before we start the loop for the fourth time, we see that 256 is no longer less than 100, bringing an end to the loop.
Do...while syntax
There is another kind of loop that continues as long as the conditions are correct: the do...while loop. It looks like this:
do {
...
} while (condition);
The previous example can be rephrased using the do...while structure:
let n = 2;
do {
n = n * n;
console.log(n);
} while (n < 100);
It's easy to see that the output will be the same as the one from while:
4
16
256
So, what separates these two, and why do we need two types of loops providing the same result?
While vs. do...while
There's indeed a difference. The following example will clarify it.
let n = 256;
while (n < 100) {
n = n * n;
console.log(n);
}
What will be the output? None. The loop only starts if the condition is true. So, what happens with do...while?
let n = 256;
do {
n = n * n;
console.log(n);
} while (n < 100);
You'll find that the console displays 65536!
The difference lies in when the check occurs. With do...while, the condition evaluation happens after executing the first iteration. This means the loop will run at least once, even if the initial condition is false.
Conclusion
This topic has introduced the concept of while loops in programming. We have explored two types of loops, namely while and do...while, and highlighted their differences. We have also learned that a perpetual true condition can lead to an infinite loop. Overall, loops are an essential part of programming and are crucial in executing repeated code blocks efficiently. With this knowledge, you can apply your knowledge and challenge yourself to create more complex programs using loops.
Always remember the conditions that will lead to a successful loop execution.