📖 Scenario: You are working on a simple program that processes a list of numbers. Sometimes, you want to skip certain numbers based on a condition and continue with the rest.
🎯 Goal: Build a JavaScript program that uses the continue statement inside a for loop to skip even numbers and print only odd numbers from a list.
📋 What You'll Learn
Create an array called numbers with the values [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].
Create a variable called skipEven and set it to true.
Use a for loop to go through each number in numbers.
Inside the loop, use an if statement to check if skipEven is true and the current number is even.
If the number is even and skipEven is true, use the continue statement to skip printing that number.
Print only the numbers that are not skipped.
💡 Why This Matters
🌍 Real World
Skipping certain items in a list is common when filtering data, such as ignoring invalid entries or focusing on specific values.
💼 Career
Understanding how to control loops with continue helps in writing efficient code for data processing, web development, and automation tasks.
Progress0 / 4 steps
1
Create the numbers array
Create an array called numbers with these exact values: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].
Javascript
Hint
Use const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; to create the array.
2
Add the skipEven variable
Create a variable called skipEven and set it to true.
Javascript
Hint
Use const skipEven = true; to create the variable.
3
Use a for loop with continue to skip even numbers
Use a for loop with the variable i to go through numbers. Inside the loop, use an if statement to check if skipEven is true and numbers[i] is even. If so, use continue to skip to the next number.
Javascript
Hint
Use for (let i = 0; i < numbers.length; i++) to loop. Use if (skipEven && numbers[i] % 2 === 0) to check even numbers and continue; to skip them.
4
Print the numbers that are not skipped
Print each number inside the loop that is not skipped by the continue statement using console.log(numbers[i]).
Javascript
Hint
Use console.log(numbers[i]) to print the numbers that are not skipped.
Practice
(1/5)
1. What does the continue statement do inside a loop in JavaScript?
easy
A. It skips the rest of the current loop iteration and moves to the next iteration.
B. It stops the entire loop immediately.
C. It restarts the whole program from the beginning.
D. It pauses the loop until a condition is met.
Solution
Step 1: Understand the role of continue in loops
The continue statement skips the remaining code inside the current loop iteration.
Step 2: Identify what happens after continue
After skipping, the loop moves directly to the next iteration without stopping the whole loop.
Final Answer:
It skips the rest of the current loop iteration and moves to the next iteration. -> Option A
Quick Check:
continue skips current step [OK]
Hint: Remember: continue skips current step, break stops loop [OK]
Common Mistakes:
Confusing continue with break
Thinking continue stops the loop
Believing continue restarts the program
2. Which of the following is the correct syntax to use continue inside a for loop in JavaScript?
easy
A. continue[];
B. continue;
C. continue loop;
D. continue()
Solution
Step 1: Recall the syntax of continue
The continue statement is used alone followed by a semicolon.
Step 2: Check each option for syntax correctness
continue; uses continue; which is correct. Options A, B, and C use invalid syntax.
Final Answer:
continue; -> Option B
Quick Check:
Correct syntax is continue; [OK]
Hint: Use continue as a statement, no parentheses or brackets [OK]
Common Mistakes:
Adding parentheses after continue
Using continue with labels incorrectly
Adding brackets or other symbols
3. What will be the output of the following code?
for (let i = 1; i <= 5; i++) {
if (i === 3) continue;
console.log(i);
}
medium
A. 1 2 3 4 5
B. 3 4 5
C. 1 2 4 5
D. 1 2
Solution
Step 1: Analyze the loop and condition
The loop runs from 1 to 5. When i is 3, continue skips the console.log.
Step 2: Determine which numbers are printed
Numbers 1, 2, 4, and 5 are printed. Number 3 is skipped.
Final Answer:
1 2 4 5 -> Option C
Quick Check:
continue skips 3, prints others [OK]
Hint: Skip printing when continue runs, others print [OK]
Common Mistakes:
Including the skipped number in output
Confusing continue with break
Ignoring the condition inside the loop
4. Find the error in this code snippet:
for (let i = 0; i < 5; i++) {
if (i = 2) continue;
console.log(i);
}
medium
A. The assignment operator (=) is used instead of comparison (===) in the if condition.
B. continue cannot be used inside for loops.
C. console.log should be before the if statement.
D. The loop condition should be i <= 5.
Solution
Step 1: Identify the if condition mistake
The condition uses i = 2 which assigns 2 to i instead of comparing.
Step 2: Understand the effect of assignment in if
Assignment returns 2 (truthy), so continue runs every time, causing an infinite loop or skipping all logs.
Final Answer:
The assignment operator (=) is used instead of comparison (===) in the if condition. -> Option A
Quick Check:
Use === for comparison, not = [OK]
Hint: Use === for comparison inside if, not = [OK]
Common Mistakes:
Using = instead of === in conditions
Thinking continue is invalid in for loops
Misplacing console.log inside loop
5. You want to print all numbers from 1 to 10 except multiples of 3 using a for loop and continue. Which code correctly achieves this?