JavaScript Continue Statement

JavaScript is a versatile programming language that offers various control flow statements to manipulate the execution of code. One such statement is the continue statement, which allows you to skip the current iteration of a loop and move on to the next one. In this article, we will explore the continue statement in JavaScript and provide examples to help you understand its usage.

Usage of the continue Statement

The continue statement is primarily used within loop structures, such as for loops, while loops, and do-while loops. When encountered, it immediately stops the execution of the current iteration and proceeds to the next iteration. This can be particularly useful when you want to skip certain iterations based on specific conditions.

Syntax

The syntax of the continue statement in JavaScript is as follows:

continue;

It is important to note that the continue statement can only be used within the body of a loop. If used outside of a loop, it will result in a syntax error.

Example 1: Using continue with a for Loop

Let’s consider a scenario where we want to print all even numbers between 1 and 10, excluding the number 6. We can achieve this using the continue statement within a for loop:

for (let i = 1; i <= 10; i++) {
    if (i === 6) {
        continue;
    }
    if (i % 2 === 0) {
        console.log(i);
    }
}

In this example, when the value of i is equal to 6, the continue statement is executed, skipping the rest of the code within the current iteration. As a result, the number 6 is excluded from the output.

Example 2: Using continue with a while Loop

The continue statement can also be used with a while loop. Let’s consider a scenario where we want to print all odd numbers between 1 and 10, excluding the number 3:

let i = 1;
while (i <= 10) {
    if (i === 3) {
        i++;
        continue;
    }
    if (i % 2 !== 0) {
        console.log(i);
    }
    i++;
}

In this example, when the value of i is equal to 3, the continue statement is executed. The i++ statement ensures that the value of i is incremented before moving on to the next iteration. As a result, the number 3 is excluded from the output.

Example 3: Using continue with a do-while Loop

The continue statement can also be used with a do-while loop. Let’s consider a scenario where we want to print all numbers between 1 and 5, excluding the number 4:

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

In this example, when the value of i is equal to 4, the continue statement is executed. The i++ statement ensures that the value of i is incremented before moving on to the next iteration. As a result, the number 4 is excluded from the output.

Conclusion

The continue statement in JavaScript provides a convenient way to skip specific iterations within loop structures. By using the continue statement, you can control the flow of your code and exclude certain values or conditions from the loop’s execution. Understanding how to utilize the continue statement effectively can help you write cleaner and more efficient code.

Scroll to Top