JavaScript tutorials for Beginners – JavaScript continue Statement

 

 

(JavaScript Tutorials for Beginners)

In this end-to-end example, you will learn – JavaScript tutorials for Beginners – JavaScript continue Statement.

JavaScript continue Statement

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

The continue statement is used to skip 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

Working of continue statement in JavaScript
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 means

  • When i is equal to 3, the continue statement skips the third iteration.
  • Then, i becomes 4 and the test condition and continue statement is evaluated again.
  • Hence, 4 and 5 are printed in the next two iterations.

 

Note: The continue statement is almost always 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 terminates the loop entirely. However, the continue statement only skips the current iteration.


continue with while loop

In a while loop, continue skips the current iteration and 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;
}
  • When the user enters a non-numeric number/string, the continue statement skips the current iteration. Then the control flow of the program goes to the condition of while loop.
  • When the user enters a number less than 0, the loop terminates.

 

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

When continue is used inside of two nested loops, continue skips the current iteration of the inner loop. For example,

// 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 inner loop and control flow of the program moves to the updateExpression of the inner loop.

Hence, the value of j = 2 is never displayed 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.

But labeled continue is rarely used in JavaScript because this makes the code harder to read and understand.

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

 

Personal Career & Learning Guide for Data Analyst, Data Engineer and Data Scientist

Applied Machine Learning & Data Science Projects and Coding Recipes for Beginners

A list of FREE programming examples together with eTutorials & eBooks @ SETScholars

95% Discount on “Projects & Recipes, tutorials, ebooks”

Projects and Coding Recipes, eTutorials and eBooks: The best All-in-One resources for Data Analyst, Data Scientist, Machine Learning Engineer and Software Developer

Topics included: Classification, Clustering, Regression, Forecasting, Algorithms, Data Structures, Data Analytics & Data Science, Deep Learning, Machine Learning, Programming Languages and Software Tools & Packages.
(Discount is valid for limited time only)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.

Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners

Please do not waste your valuable time by watching videos, rather use end-to-end (Python and R) recipes from Professional Data Scientists to practice coding, and land the most demandable jobs in the fields of Predictive analytics & AI (Machine Learning and Data Science).

The objective is to guide the developers & analysts to “Learn how to Code” for Applied AI using end-to-end coding solutions, and unlock the world of opportunities!