The continue statement helps skip the rest of the current loop cycle and move to the next one. It lets you ignore certain steps without stopping the whole loop.
When you want to skip processing some items in a list but keep looping through the rest.
When checking conditions inside a loop and you want to ignore some cases quickly.
When filtering data inside a loop and only want to act on certain values.
When you want to avoid nested if-else blocks by skipping unwanted cases early.
Syntax
C++
continue;
The continue statement is used inside loops like for, while, or do-while.
It immediately jumps to the next iteration of the loop, skipping any code below it in the current cycle.
Examples
This loop skips printing the number 2 by using continue.
C++
for (int i = 0; i < 5; i++) {
if (i == 2) {
continue;
}
std::cout << i << " ";
}
In this while loop, when i is 3, it skips printing and goes to the next iteration.
C++
int i = 0;
while (i < 5) {
i++;
if (i == 3) continue;
std::cout << i << " ";
}
Sample Program
This program prints numbers from 1 to 6 but skips 3 and 5 using the continue statement.
C++
#include <iostream>
int main() {
std::cout << "Numbers except 3 and 5:\n";
for (int i = 1; i <= 6; i++) {
if (i == 3 || i == 5) {
continue;
}
std::cout << i << " ";
}
std::cout << std::endl;
return0;
}
OutputSuccess
Important Notes
Using continue can make your loops cleaner by avoiding deep nesting of if statements.
Be careful not to create infinite loops by skipping the part that changes the loop variable.
Summary
continue skips the rest of the current loop cycle and moves to the next.
It works inside for, while, and do-while loops.
Use it to ignore certain cases without stopping the whole loop.
Practice
(1/5)
1. What does the continue statement do inside a loop in C++?
easy
A. It stops the entire loop immediately.
B. It skips the rest of the current loop iteration and moves to the next iteration.
C. It exits the current function.
D. It restarts the program from the beginning.
Solution
Step 1: Understand the role of continue in loops
The continue statement tells the program to skip the remaining code in the current loop cycle.
Step 2: Identify what happens after continue
After skipping, the loop moves to the next iteration immediately without executing the rest of the loop body.
Final Answer:
It skips the rest of the current loop iteration and moves to the next iteration. -> Option B
Quick Check:
Continue skips current iteration [OK]
Hint: Continue skips current loop step, not whole loop [OK]
Common Mistakes:
Thinking continue stops the loop completely
Confusing continue with break
Assuming continue exits the function
2. Which of the following is the correct syntax to use continue inside a for loop in C++?
easy
A. continue;
B. continue()
C. continue{}
D. continue[]
Solution
Step 1: Recall the syntax of continue in C++
The continue statement is a keyword and used alone followed by a semicolon.
Step 2: Check each option's syntax
Only continue; is valid. The others use parentheses or braces which are incorrect.
Final Answer:
continue; -> Option A
Quick Check:
Continue ends with semicolon [OK]
Hint: Continue is a keyword, ends with semicolon [OK]
Common Mistakes:
Adding parentheses after continue
Using braces or brackets with continue
Omitting the semicolon
3. What will be the output of the following C++ code?
for (int i = 1; i <= 5; i++) {
if (i == 3) continue;
std::cout << i << " ";
}
medium
A. 1 2 3 4 5
B. 1 2 3 4
C. 1 2 4 5
D. 2 3 4 5
Solution
Step 1: Analyze the loop and condition
The loop runs from 1 to 5. When i == 3, the continue skips printing.
Step 2: Determine printed values
Numbers 1 and 2 print normally. At 3, continue skips printing. Then 4 and 5 print.