looping of javascript

Looping of Javascript

If you are a beginner in Javascript, you might have heard the term "looping" quite often. Looping is a process of executing a set of instructions repeatedly while a certain condition is true. In Javascript, there are mainly two types of loop statements - "for" loop and "while" loop. Let's take a closer look at each of them.

The For Loop

The "for" loop is used when you know exactly how many times you want to execute a set of instructions. Here is the basic syntax of a "for" loop:


for (initialization; condition; increment/decrement) {
  // code to be executed
}

The "initialization" part is executed once at the beginning of the loop. It usually declares and initializes a variable that will be used in the loop. The "condition" part is tested before each iteration of the loop. If it evaluates to true, the loop will continue. If it evaluates to false, the loop will stop. The "increment/decrement" part is executed after each iteration of the loop. It usually increments or decrements the variable declared in the "initialization" part.

Let's see an example:


for (var i = 0; i < 5; i++) {
  console.log(i);
}

This "for" loop will iterate 5 times, from 0 to 4, and print out the value of the variable "i" in each iteration.

The While Loop

The "while" loop is used when you don't know exactly how many times you want to execute a set of instructions. Here is the basic syntax of a "while" loop:


while (condition) {
  // code to be executed
}

The "condition" part is tested before each iteration of the loop. If it evaluates to true, the loop will continue. If it evaluates to false, the loop will stop.

Let's see an example:


var i = 0;
while (i < 5) {
  console.log(i);
  i++;
}

This "while" loop will iterate 5 times, from 0 to 4, and print out the value of the variable "i" in each iteration.

The Do...While Loop

The "do...while" loop is similar to the "while" loop, but the code inside the loop will always execute at least once, even if the condition is false. Here is the basic syntax of a "do...while" loop:


do {
  // code to be executed
} while (condition);

Let's see an example:


var i = 0;
do {
  console.log(i);
  i++;
} while (i < 5);

This "do...while" loop will iterate 5 times, from 0 to 4, and print out the value of the variable "i" in each iteration.

Conclusion

Looping is a fundamental concept in programming, and Javascript provides different types of loop statements to help you execute a set of instructions repeatedly. The "for" loop is used when you know exactly how many times you want to execute the instructions, the "while" loop is used when you don't know exactly how many times you want to execute the instructions, and the "do...while" loop is similar to the "while" loop but executes the code inside the loop at least once. By using these loop statements, you can make your code more efficient and flexible.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe