in , ,

JavaScript continue Statement

JavaScript continue Statement
JavaScript continue Statement

JavaScript continue Statement

In this tutorial, you will learn about the continue statement with the assistance of examples.

In this article, you will learn-

Definition and Usage

JavaScript continue Statement

The continue statement breaks one iteration (in the loop) if a predefined condition happens, and continues with the next iteration in the loop.

The distinction among continue and the break statement, is as opposed to “leaping out” of a loop, the continue statement “jumps over” one iteration in the loop.

In any case, when the continue statement is executed, it acts distinctively for various types of loops:

In a while loop, the condition is tried, and if it is true, the loop is executed once more

In a for loop, the increment expression (for example i++) is first evaluated, and afterward the condition is tried to find out if another iteration should be finished

The continue statement can likewise be used with an optional label reference.

Note: The continue statement (with or without a label reference) can only be used inside a loop.


The continue statement is used to avoid the current iteration of the loop and the control flow of the program goes to the next iteration.

The syntax of the continue statement is:

continue [label];

Note: label is optional and rarely used.


Working of JavaScript continue Statement


continue with for Loop

In a for loop, continue skips the current iteration and control flow jumps to the updateExpression.


Example 1: Print the Value of i

// program to print the value of i
for (let i = 1; i <= 5; i++) {

    // condition to continue    
    if (i == 3) {
        continue;
    }

    console.log(i);
}

Output

1
2
4
5

In the above program, for loop is used to print the value of i in each iteration.

Notice the continue statement inside the loop.

if(i == 3) {
    continue;
}

This implies

  • At the point when i is equivalent to 3, the continue statement skips the third iteration.
  • At that point, I become 4, and the test condition and continue statement is evaluated once more.
  • Thus, 4 and 5 are printed in the next two iterations.

Note: The continue statement is quite often used with decision-making statements. To learn more, visit JavaScript if…else Statement.

To learn more about for loop, visit JavaScript for loop.

Note: The break statement ends the loop completely. Be that as it may, the continue statement just skips the current iteration.


continue with while Loop

In a while loop, continue skips the current iteration, and the control flow of the program jumps back to the while condition.

The continue statement works in the same way for while and do…while loops.


Example 2: Calculate Positive Number

// program to calculate positive numbers only
// if the user enters a negative number, that number is skipped from calculation

// negative number -> loop terminate
// non-numeric character -> skip iteration

let sum = 0;
let number = 0;

while (number >= 0) {

    // add all positive numbers
    sum += number;

    // take input from the user
    number = parseInt(prompt('Enter a number: '));

    // continue condition
    if (isNaN(number)) {
        console.log('You entered a string.');
        number = 0; // the value of number is made 0 again
        continue;
    }

}

// display the sum
console.log(`The sum is ${sum}.`);

Output

Enter a number: 1
Enter a number: 2
Enter a number: hello
You entered a string.
Enter a number: 5
Enter a number: -2
The sum is 8. 

In the above program, the user enters a number. The while loop is used to print the total sum of positive numbers entered by the user.

Notice the use of the continue statement.

if (isNaN(number)) {
    continue;
}
  • At the point when the user enters a non-numeric number/string, the continue statement skips the current iteration. At that point the control flow of the program goes to the condition of while loop.
  • At the point when the user enters a number less than 0, the loop ends.

In the above program, isNaN() is used to check if the value entered by a user is a number or not.

To learn more about the while loop, visit JavaScript while loop.


Continue with Nested Loop

At the point when continue is used within two nested loops, continue skips the current iteration of the inward loop. For instance,

// nested for loops

// first loop
for (let i = 1; i <= 3; i++) {

    // second loop
    for (let j = 1; j <= 3; j++) {
        if (j == 2) {
          continue;
        }
        console.log(`i = ${i}, j = ${j}`);
    }
}

Output

i = 1, j = 1
i = 1, j = 3
i = 2, j = 1
i = 2, j = 3
i = 3, j = 1
i = 3, j = 3

In the above program, when the continue statement executes, it skips the current iteration in the internal loop and control flow of the program moves to the updateExpression of the inward loop.

Thus, the value of j = 2 is never shown in the output.


JavaScript Labeled continue

When using nested loops, you can skip the current iteration and the control flow of the program can be passed to a label statement’s updateExpression.

Be that as it may, labeled continue is once in a while used in JavaScript on the grounds that this makes the code harder to read and comprehend.

If you want to learn more about the labeled continue statements, visit labeled continue.


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

WhatsApp to stop working on these smartphones from January 1

WhatsApp to stop working on these smartphones from January 1

How to Set Up Two WhatsApp Accounts on Android

The most effective method to Set Up Two WhatsApp Accounts on Android