The continue statement in JavaScript is used to skip the remaining statements in the current loop iteration and move to the next iteration. It is useful when certain values or conditions should be ignored without terminating the entire loop. The continue statement works with for, while, do...while, for...in, and for...of loops.
Table of Contents
What Is the continue Statement in JavaScript?
The continue statement skips the remaining code in the current iteration of a loop and proceeds to the next iteration. Unlike the break statement, it does not terminate the entire loop.
It is useful when you want to skip specific values while continuing to process the remaining items.
Example:
// JavaScript program to skip the number 3 using continue
for (let i = 1; i <= 5; i++) {
if (i === 3) {
continue;
}
console.log(i);
}
Output:
1
2
4
5
In this example, the loop skips the number 3 because the continue statement executes when i === 3. The loop then proceeds with the next iteration and prints the remaining numbers.
Syntax of the continue Statement
The syntax of the continue statement is simple.
Syntax:
continue;
The statement can also include a label to skip to the next iteration of a specific enclosing labeled loop.
Example:
// JavaScript program demonstrating the continue statement
for (let i = 1; i <= 4; i++) {
if (i === 2) {
continue;
}
console.log("Number:", i);
}
Output:
Number: 1
Number: 3
Number: 4
When i equals 2, the continue statement skips the console.log() statement for that iteration. The loop continues with the next value.
How the continue Statement Works
The continue statement changes the normal execution of a loop by skipping the remaining statements in the current iteration.
The following steps explain the general behavior of continue:
- The loop begins and checks its condition.
- If the condition is true, the loop body executes.
- When the continue statement is reached, the remaining statements in that iteration are skipped.
- The loop proceeds to its next iteration.
- The process repeats until the loop condition becomes false or the loop terminates in another way.
For a traditional for loop, the update expression executes after continue, before the next condition check. In a while loop, execution returns to the condition check, so any required variable update must be handled carefully.
Examples of the continue Statement
Example 1: Skip an even number.
// JavaScript program to print odd numbers using continue
for (let i = 1; i <= 10; i++) {
if (i % 2 === 0) {
continue;
}
console.log(i);
}
Output:
1
3
5
7
9
The condition i % 2 === 0 identifies even numbers. Whenever the condition is true, continue skips the output statement, so only odd numbers are printed.
Example 2: Skip a value in a while loop.
// JavaScript program to skip the number 3 in a while loop
let i = 0;
while (i < 5) {
i++;
if (i === 3) {
continue;
}
console.log(i);
}
Output:
1
2
4
5
The variable is incremented before the condition that triggers continue. This ensures the loop progresses even when the number 3 is skipped.
Example 3: Skip an item in an array.
// JavaScript program to skip a specific fruit using continue
const fruits = ["Apple", "Banana", "Mango", "Orange"];
for (const fruit of fruits) {
if (fruit === "Mango") {
continue;
}
console.log(fruit);
}
Output:
Apple
Banana
Orange
The loop skips "Mango" but continues processing the remaining fruits.
Example 4: Skip a number in a do...while loop.
// JavaScript program to skip the number 2 using continue
let count = 0;
do {
count++;
if (count === 2) {
continue;
}
console.log(count);
} while (count < 4);
Output:
1
3
4
The loop skips printing 2. Since count is incremented before continue, the loop condition can still be checked and the loop can progress normally.
Using continue in Different Loops
The continue statement works with different JavaScript loop types. Its purpose remains the same, but the way control proceeds to the next iteration depends on the loop type.
Using continue with a for loop:
// JavaScript program to skip numbers less than 3
for (let i = 1; i <= 5; i++) {
if (i < 3) {
continue;
}
console.log(i);
}
Output:
3
4
5
The loop skips the first two values and prints the numbers from 3 to 5.
Using continue with a for...of loop:
// JavaScript program to print positive numbers only
const numbers = [-2, 5, 0, 8, -1];
for (const number of numbers) {
if (number <= 0) {
continue;
}
console.log(number);
}
Output:
5
8
The loop skips zero and negative numbers and prints only positive values.
Using continue with a for...in loop:
// JavaScript program to skip the age property
const student = {
name: "Riya",
age: 21,
course: "IT"
};
for (const key in student) {
if (key === "age") {
continue;
}
console.log(`${key}: ${student[key]}`);
}
Output:
name: Riya
course: IT
The loop skips the age property and processes the other enumerable property keys. In general, for...in can also visit inherited enumerable properties.
Labeled continue Statement in JavaScript
A labeled continue statement allows a program to skip to the next iteration of a specific enclosing labeled loop. It is particularly useful in nested loops when the next iteration of an outer loop should begin immediately.
Example:
// JavaScript program to use labeled continue in nested loops
outerLoop:
for (let i = 1; i <= 3; i++) {
for (let j = 1; j <= 3; j++) {
if (j === 2) {
continue outerLoop;
}
console.log(i, j);
}
}
Output:
1 1
2 1
3 1
When j becomes 2, continue outerLoop skips the remaining work in the current outer-loop iteration and proceeds to the next value of i. Therefore, only the first inner-loop value is printed for each outer-loop iteration.
A labeled continue must refer to an enclosing labeled iteration statement, such as a loop. It cannot target an ordinary labeled block or a switch statement.
Difference Between break and continue
The break and continue statements control loop execution, but they have different effects.
| Feature | break | continue |
|---|---|---|
| Purpose | Terminates the current loop | Skips the current iteration |
| Loop execution | Ends immediately | Continues with the next iteration |
| Statements after the keyword in the loop body | Skipped | Skipped for the current iteration |
| Common use | Stop searching after finding a value | Skip unwanted values |
| Works in a switch statement | Yes | No, not as a way to skip a switch case |
Example using break:
// JavaScript program to terminate a loop using break
for (let i = 1; i <= 5; i++) {
if (i === 3) {
break;
}
console.log(i);
}
Output:
1
2
The loop terminates when i reaches 3.
Example using continue:
// JavaScript program to skip one iteration using continue
for (let i = 1; i <= 5; i++) {
if (i === 3) {
continue;
}
console.log(i);
}
Output:
1
2
4
5
The loop skips 3 but continues with the remaining values.
Advantages of the continue Statement
The continue statement offers several benefits when used appropriately:
- Skips unwanted values: It prevents unnecessary processing of items that do not meet a condition.
- Improves readability: It can make filtering logic easier to understand by handling skip conditions early.
- Keeps loops running: It allows the program to process later iterations without terminating the entire loop.
- Supports multiple loop types: It works with common JavaScript loops, including for, while, and for...of.
- Helps simplify conditional logic: It can reduce deeply nested if statements when certain cases should be skipped.
Use continue when skipping an iteration makes the logic clearer. For more complex data processing, methods such as filter() and map() may sometimes be more suitable.
Common Mistakes to Avoid
-
Forgetting to update a while-loop variable: If continue skips the statement that updates the variable, the loop may run indefinitely. Place necessary updates before the continue or restructure the loop carefully.
-
Confusing continue with break: The continue statement skips one iteration, while break terminates the current loop.
-
Placing continue outside a loop: A continue statement must be inside a loop. Using it outside a valid loop context causes a syntax error.
-
Using an invalid label: A labeled continue must refer to an enclosing labeled loop. It cannot target a regular block or a switch statement.
-
Expecting skipped statements to execute: Once continue runs, the remaining statements in the current iteration are skipped, so place important operations before it when they must always run.
Frequently Asked Questions
1. What is the continue statement in JavaScript?
The continue statement skips the remaining statements in the current loop iteration and proceeds to the next iteration.
2. What is the difference between break and continue?
break terminates the current loop, whereas continue skips only the current iteration and allows the loop to continue.
3. Can continue be used in a while loop?
Yes. The continue statement can be used in a while loop, but you must ensure that required variable updates are not accidentally skipped.
4. Can continue be used in a switch statement?
No. The continue statement cannot be used to skip a switch case. It must target an enclosing loop, either directly or through a valid label.
5. What is a labeled continue statement?
A labeled continue statement skips to the next iteration of a specifically labeled enclosing loop. It is useful for controlling nested loops.
0 Comments