Javascript continue Statement

Introduction

The continue statements provide control over the loop statement.

The continue statement exits the loop immediately, but execution continues from the top of the loop.

The following code uses continue statement:

let num = 0;//from w  w w .j av a 2  s  .  co m

for (let i = 1; i < 10; i++) {
    if (i % 5 == 0) {
        continue;
    }
    num++;
}

console.log(num); // 8 

Here, the console.log displays 8.

When i reaches a value of 5, the loop is exited before num is incremented.

The execution continues with the next iteration, when the value is 6.

The loop then continues until its natural completion, when i is 10.

The continue statements can be used in conjunction with labeled statements to return to a particular location in the code.

let num = 0;/*  ww  w. ja va 2  s .co  m*/

outermost:
    for (let i = 0; i < 10; i++) {
        for (let j = 0; j < 10; j++) {
            if (i == 5 && j == 5) {
                continue outermost;
            }
            num++;
        }
    }

console.log(num); // 95 



PreviousNext

Related