In this chapter, we will learn about the JavaScript continue
statement. The continue
statement is used to skip the current iteration of a loop and continue with the next iteration. We will cover:
- What is the
continue
Statement? - Syntax
- Using the
continue
Statement in Loops - Skipping Iterations in Nested Loops
- Simple Programs using the
continue
Statement
What is the continue Statement?
The continue
statement is used within loops to skip the current iteration and continue with the next iteration of the loop. It allows you to control the flow of the loop and avoid executing certain parts of the loop body based on a condition.
Syntax
continue;
Using the continue Statement in Loops
Example
// Loop through numbers from 0 to 9
for (let i = 0; i < 10; i++) {
// Check if the current number is 5
if (i === 5) {
// Skip the rest of the loop body and continue with the next iteration
continue;
}
// Print the current number
console.log(i);
}
Output:
0
1
2
3
4
6
7
8
9
In the example above, the continue
statement skips the iteration when i
is 5, and the loop continues with the next iteration.
Skipping Iterations in Nested Loops
Example
// Outer loop
for (let i = 0; i < 3; i++) {
// Inner loop
for (let j = 0; j < 3; j++) {
// Check if the inner counter is 1
if (j === 1) {
// Skip the rest of the loop body and continue with the next iteration of the inner loop
continue;
}
// Print the values of the counters
console.log(`i = ${i}, j = ${j}`);
}
}
Output:
i = 0, j = 0
i = 0, j = 2
i = 1, j = 0
i = 1, j = 2
i = 2, j = 0
i = 2, j = 2
In the example above, the continue
statement skips the iteration of the inner loop when j
is 1, and the inner loop continues with the next iteration.
Simple Programs using the continue Statement
Program 1: Print Odd Numbers from 1 to 10
// Loop through numbers from 1 to 10
for (let i = 1; i <= 10; i++) {
// Check if the current number is even
if (i % 2 === 0) {
// Skip the rest of the loop body and continue with the next iteration
continue;
}
// Print the current odd number
console.log(i);
}
Output:
1
3
5
7
9
Program 2: Skip Multiples of 3 in an Array
// Define an array of numbers
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
// Loop through the array
for (let i = 0; i < numbers.length; i++) {
// Check if the current number is a multiple of 3
if (numbers[i] % 3 === 0) {
// Skip the rest of the loop body and continue with the next iteration
continue;
}
// Print the current number
console.log(numbers[i]);
}
Output:
1
2
4
5
7
8
10
Program 3: Skip Characters in a String
// Define a string
let str = "hello world";
// Define a character to skip
let charToSkip = 'o';
// Loop through the string
for (let i = 0; i < str.length; i++) {
// Check if the current character is the character to skip
if (str[i] === charToSkip) {
// Skip the rest of the loop body and continue with the next iteration
continue;
}
// Print the current character
console.log(str[i]);
}
Output:
h
e
l
l
w
r
l
d
Program 4: Sum of Non-Negative Numbers in an Array
// Define an array of numbers
let numbers = [1, -2, 3, -4, 5];
// Initialize the sum variable
let sum = 0;
// Loop through the array
for (let i = 0; i < numbers.length; i++) {
// Check if the current number is negative
if (numbers[i] < 0) {
// Skip the rest of the loop body and continue with the next iteration
continue;
}
// Add the current number to the sum
sum += numbers[i];
}
// Print the sum of non-negative numbers
console.log("Sum of non-negative numbers:", sum);
Output:
Sum of non-negative numbers: 9
Program 5: Filter Non-Prime Numbers from an Array
// Define a function to check if a number is prime
function isPrime(num) {
if (num <= 1) return false;
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) return false;
}
return true;
}
// Define an array of numbers
let numbers = [2, 3, 4, 5, 6, 7, 8, 9, 10];
// Initialize an array to store prime numbers
let primes = [];
// Loop through the array
for (let i = 0; i < numbers.length; i++) {
// Check if the current number is not prime
if (!isPrime(numbers[i])) {
// Skip the rest of the loop body and continue with the next iteration
continue;
}
// Add the current prime number to the primes array
primes.push(numbers[i]);
}
// Print the array of prime numbers
console.log("Prime numbers:", primes);
Output:
Prime numbers: [2, 3, 5, 7]
Conclusion
In this chapter, you learned about the JavaScript continue
statement, including its syntax and how to use it in loops. We also covered skipping iterations in nested loops and provided various use cases with simple programs to demonstrate the usage of the continue
statement. The continue
statement is used for controlling the flow of your loops and skipping specific iterations based on conditions.