xxxxxxxxxx
/*continue is a keyword in JavaScript that is used inside loops to skip the
execution of the remaining statements in the current iteration of the loop
and move to the next iteration. */
// Here's an example:
for (let i = 0; i < 5; i++) {
if (i === 2) {
continue;
}
console.log(i);
}
// returns 0 1 3 4
xxxxxxxxxx
let i = 0;
let n = 0;
while (i < 5) {
i++;
if (i === 3) {
continue;
}
n += i;
}
xxxxxxxxxx
// program to print the value of i
for (let i = 1; i <= 5; i++) {
// condition to continue
if (i == 3) {
continue;
}
console.log(i);
}
xxxxxxxxxx
for (let i = 1; i <= 10; i++) {
if (i == 3 || i == 7) {
continue;
}
console.log(i);
}
xxxxxxxxxx
for (var i = 0; i < 10; i++) {
if (i == 5) { continue; } // skips the rest of the cycle
document.write(i + ", "); // skips 5
}
xxxxxxxxxx
// The continue statement allows us to skip the current iteration
// of a loop
const arr = [1, 2, 3]
for(let num of arr) {
if(num === 2) continue;
console.log(num);
} // logs: 1 3
xxxxxxxxxx
for (let i = 0; i < 10; i++) {
if (i === 4) {
continue; // Skips the current iteration when i is 4
}
console.log(i);
}
xxxxxxxxxx
// 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}.`);