JavaScript(JS) continue Statement

www.i‮editfig‬a.com

JavaScript continue Statement

In JavaScript, the continue statement is used to skip the current iteration of a loop and move on to the next iteration. The continue statement works with loops such as for, while, and do-while.

Here's an example of how to use the continue statement in a for loop:

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

In this example, the continue statement is used to skip the iteration when the variable i is equal to 3. The console will print the following output:

1
2
4
5

As you can see, the iteration when i is equal to 3 is skipped, and the loop continues with the next iteration. The continue statement can be useful when you want to skip a certain iteration of a loop based on a condition.

You can also use the continue statement with a while loop or a do-while loop. Here's an example using a while loop:

let i = 0;
while (i < 5) {
  i++;
  if (i === 3) {
    continue;
  }
  console.log(i);
}

In this example, the continue statement is used to skip the iteration when i is equal to 3. The console will print the following output:

1
2
4
5

As you can see, the continue statement works the same way in a while loop as it does in a for loop.

continue with for Loop

Certainly! Here's an example of using the continue statement with a for loop:

for (let i = 0; i < 5; i++) {
  if (i === 2) {
    continue;
  }
  console.log(i);
}

In this example, the continue statement is used to skip the iteration when the variable i is equal to 2. The console will print the following output:

0
1
3
4

As you can see, the iteration when i is equal to 2 is skipped, and the loop continues with the next iteration.

You can also use the continue statement with nested for loops. Here's an example:

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

In this example, the continue statement is used to skip the iteration of the inner loop when the variable j is equal to 2. The console will print the following 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

As you can see, when j is equal to 2, the iteration of the inner loop is skipped, and the loop continues with the next iteration of j.

continue with while Loop

Certainly! Here's an example of using the continue statement with a while loop:

let i = 0;

while (i < 5) {
  i++;
  if (i === 2) {
    continue;
  }
  console.log(i);
}

In this example, the continue statement is used to skip the iteration when the variable i is equal to 2. The console will print the following output:

1
3
4
5

As you can see, the iteration when i is equal to 2 is skipped, and the loop continues with the next iteration.

You can also use the continue statement with nested while loops. Here's an example:

let i = 1;
while (i <= 3) {
  let j = 1;
  while (j <= 3) {
    j++;
    if (j === 2) {
      continue;
    }
    console.log(`i: ${i}, j: ${j}`);
  }
  i++;
}

In this example, the continue statement is used to skip the iteration of the inner loop when the variable j is equal to 2. The console will print the following 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

As you can see, when j is equal to 2, the iteration of the inner loop is skipped, and the loop continues with the next iteration of j.

continue with Nested Loop

Certainly! Here's an example of using the continue statement with a nested for loop:

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

In this example, the continue statement is used to skip the iteration of the inner loop when the variable j is equal to 2. The console will print the following 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

As you can see, when j is equal to 2, the iteration of the inner loop is skipped, and the loop continues with the next iteration of j.

You can also use the continue statement with multiple nested loops, such as a for loop inside a while loop. The same concept applies: when the continue statement is encountered, the current iteration of the inner loop is skipped, and the loop continues with the next iteration.

JavaScript Labeled continue

In JavaScript, you can use a labeled continue statement to continue to the next iteration of an outer loop instead of an inner loop. Here's an example:

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

In this example, the continue statement is labeled with the identifier outerLoop, which is the name of the outer for loop. When j is equal to 2, the labeled continue statement is executed, causing the current iteration of the outer loop to be skipped and continuing with the next iteration of the outer loop. This means that the inner loop stops executing and the next iteration of the outer loop begins.

The console will print the following output:

i: 1, j: 1
i: 2, j: 1
i: 3, j: 1
i: 3, j: 3

As you can see, the inner loop iteration with j = 2 is skipped, and the loop continues with the next iteration of the outer loop.