Web Development
MEAN-Stack from A to Z
Fundamentals
Level1
for loop
The first type of loop is the "for" loop, which repeats a block of code until a certain condition is met. It is
written as follows:
for(initialization; condition; increment/decrement)
{
// Code to be executed
}
For example, if we want to print the word "hello" in the console 3 times, we can write the following code:
for(var i = 0; i <= 2; i++){
console.log("hello");
}
In this code, a variable named "i" is initialized with a value of 0, and the condition is set to "i <= 2." The
variable "i" is incremented by 1 in each iteration. Thus, the loop will run when "i" is 0, 1, and 2,
printing "hello" in the console 3 times. We can also print something
like "hello loop no 1," "hello loop no 2," "hello loop no 3" using the same variable. However, if we print the
variable directly, it will output "hello loop 0," "hello loop 1," "hello loop 2" because the variable starts from
0. Therefore, we need to increment the variable before printing it, while avoiding a common mistake like the
following code:
for(var i = 0; i <= 2; i++){
console.log("hello loop " + ++i);
}
In the above code, when executed, it will only print "hello loop 1" and "hello loop 3." Why? In the "for" loop, the
code increments the variable "i" by 1 in each iteration, as we specified. Since we don't want the count to start from
0 (for example, if we have a list of employees, it doesn't make sense for the first employee to be numbered 0), we
indeed want to increment the variable "i" by 1 before printing it. However, in the above code, when "i" is incremented
in the first iteration, at the start of the second iteration, the value of "i" becomes 2 instead of 1, and it
increments again during printing to become 3 at the end of the second iteration. Thus, at the start of the third
iteration, the value of "i" exceeds the specified condition, so the third iteration does not occur. Therefore, it is
incorrect to increment the variable as in the previous example when printing to achieve the desired goal. To achieve
the desired goal, the code should be modified as follows:
for(var i = 0; i <= 2; i++){
var x = i + 1;
console.log("hello loop " + x);
}
In the above code, the variable "x" is always one greater than the variable "i" before printing. The variable "x" does
not affect the number of iterations, so the variable "i" is incremented by only 1, not 2 as in the previous code. This
results in printing "hello loop 1," "hello loop 2," and "hello loop 3" exactly as we wanted.
While loop
Another form of loop is the "while" loop. It differs from the "for" loop in some ways. For example, in the "for" loop,
the variable is defined and initialized within the loop, and the condition and the number of increments are specified
within the loop. However, in the "while" loop, the variable is defined outside the loop, the condition is specified
inside the loop, and the number of increments is specified within the code block executed after the "while" statement,
as shown in the following example, which prints "hello" in the console 3 times:
var i = 0;
while(i < 3){
console.log('hello');
i++;
}
do while loop
The "do while" loop is similar to the "while" loop but inverted. Let's see the difference now. In the "while" loop, we
define the variable and assign it an initial value, then write the "while" statement with the condition that must be
met for the loop to occur. So, what if the condition is not met? For example, in the following code:
var i = 4;
while(i < 3){
console.log('hello');
i++;
}
The condition can never be met because the initial value is already greater than the number required for the loop to
start.
In some cases, we want the code inside the loop to be executed at least once before checking the condition. This is
where the "do while" loop comes in handy. It inverts the process by executing the code first and then checking the
condition, as shown in the following example:
var i = 4;
do{
console.log('hello world');
i++;
}
while(i<3)
In this code, the phrase "hello world" will be printed once in the console before the "while" statement detects that
the condition is not met and terminates the loop.