Skip to main content

Loop Statements (for, while, do while)

Understanding Loop Statements in JavaScript

In programming, there are often tasks that require repetition. For instance, you might need to print a message multiple times or iterate through an array of items. This is where loop statements come into play. Loop statements are control structures that allow you to execute a block of code repeatedly until a certain condition is met.

In JavaScript, there are three primary types of loops: for, while, and do while.

Let's dive in and understand each one of them.

The for Loop

A for loop is used when you know exactly how many times you want to loop through a block of code. Here is the syntax for a for loop:

for (initialExpression; condition; incrementExpression) {
// code block to be executed
}
  • initialExpression: This is executed once before the execution of the code block. It is usually used for variable initialization.
  • condition: If the condition is true, the loop will start over again; if it is false, the loop will end.
  • incrementExpression: This is executed each time after the code block has been executed. It is usually used to update the loop counter.

Here is an example of a for loop that prints numbers from 1 to 5:

for (let i = 1; i <= 5; i++) {
console.log(i);
}

The while Loop

A while loop is used when you want to loop through a block of code an unknown number of times until a specified condition is met. Here is the syntax for a while loop:

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

The loop will continue to execute the code block as long as the condition is true. Once the condition becomes false, the loop ends.

Here’s an example of a while loop that prints numbers from 1 to 5:

let i = 1;
while (i <= 5) {
console.log(i);
i++;
}

The do while Loop

The do while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true. Then it will repeat the loop as long as the condition is true. Here is the syntax for a do while loop:

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

Here's an example of a do while loop that prints numbers from 1 to 5:

let i = 1;
do {
console.log(i);
i++;
}
while (i <= 5);

Loop Control Statements

There are two important loop control statements: break and continue.

  • break: It is used to 'jump out' of a loop. When a break statement is encountered inside a loop, the loop is immediately terminated, and the program control resumes at the next statement following the loop.

  • continue: It is used to 'jump over' one iteration in the loop. It continues with the next iteration of the loop.

Here is a simple example demonstrating both:

for (let i = 1; i <= 10; i++) {
if (i === 6) {
break; // It will stop the loop when i equals 6
}
if (i === 3) {
continue; // It will skip the loop iteration when i equals 3
}
console.log(i);
}

Conclusion

Looping is a fundamental concept in programming languages. It allows us to handle repeated tasks efficiently. In JavaScript, we have for, while, and do while loops at our disposal. Each of them has its own use-cases, and understanding when to use which is crucial for writing efficient and readable code. Happy Coding!