continue;The continue Statement in C++: Skipping Selected Iterations
Direct Answer & Core Concept
continue statement in C++ is used to skip the remaining statements in the current loop iteration and move to the next iteration. It is useful when you want a loop to ignore specific values or conditions without stopping the entire loop.For example, suppose you want to print numbers from 1 to 10 but skip the number 5. Instead of ending the loop, you can use the continue statement to skip that particular iteration.
Detailed Explanation & Mechanics
1. What Is the continue Statement in C++?
The continue statement is a jump statement that skips the remaining statements in the current iteration of a loop.
When C++ encounters continue:
The remaining statements in the current iteration are skipped.
The loop moves towards its next iteration.
The loop's condition is checked again when required.
Syntax
The continue statement can be used inside:
forloopswhileloopsdo-whileloopsRange-based
forloops
It cannot be used in a switch statement by itself because switch is not a loop.
2. Simple Example of continue in C++
Let's start with a simple example that prints numbers from 1 to 5 but skips the number 3.
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
cout << i << " ";
}
return 0;
}Output:
1 2 4 5Explanation:
The loop starts with
i = 1.The program prints 1 and 2.
When
ibecomes 3, the conditioni == 3becomes true.The
continuestatement skips thecoutstatement for that iteration.The loop continues with 4 and 5.
The important point is that the loop does not terminate when continue executes.
3. Using continue in a for Loop
A for loop is commonly used when you know how many times a task needs to be repeated.
The continue statement skips the remaining statements in the current iteration. In a traditional for loop, execution then moves to the update expression before checking the loop condition again.
Example: Skip Even Numbers
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue;
}
cout << i << " ";
}
return 0;
}Output:
1 3 5 7 9Explanation:
The loop runs from 1 to 10.
The condition
i % 2 == 0checks whether a number is even.If the number is even,
continueskips the print statement.Only odd numbers are displayed.
Example: Skip a Specific Number
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 7; i++) {
if (i == 4) {
continue;
}
cout << "Number: " << i << endl;
}
return 0;
}Output:
Number: 1
Number: 2
Number: 3
Number: 5
Number: 6
Number: 7Here, the loop skips 4 but continues processing all the other numbers.
4. Using continue in a while Loop
A while loop executes as long as its condition remains true.
When continue is executed inside a while loop, the remaining statements in the current iteration are skipped, and the loop condition is checked again.
Example: Skip Number 5
#include <iostream>
using namespace std;
int main() {
int i = 1;
while (i <= 7) {
if (i == 5) {
i++;
continue;
}
cout << i << " ";
i++;
}
return 0;
}Output:
1 2 3 4 6 7Explanation:
The variable
istarts at 1.The loop prints numbers until it reaches 5.
When
iis 5, the program incrementsiand executescontinue.The print statement is skipped for that iteration.
The loop continues with 6 and 7.
Important: In a while loop, make sure your loop variable is updated before executing continue when the update would otherwise be skipped. Forgetting this can create an infinite loop.
5. Using continue in a do-while Loop
A do-while loop executes its body at least once before checking the loop condition.
When continue executes inside a do-while loop, control moves to the condition check at the end of the loop.
Example: Skip Even Numbers
#include <iostream>
using namespace std;
int main() {
int i = 0;
do {
i++;
if (i % 2 == 0) {
continue;
}
cout << i << " ";
} while (i < 10);
return 0;
}Output:
1 3 5 7 9Explanation:
The variable
istarts at 0.It is incremented at the beginning of each iteration.
If the number is even,
continueskips the print statement.The loop condition is checked.
The loop continues until
ireaches 10.
6. Using continue in a Range-Based for Loop
A range-based for loop is used to process each element in an array, container, or other suitable range.
You can use continue to skip selected elements.
Example: Skip a Specific Array Element
#include <iostream>
using namespace std;
int main() {
int numbers[] = {10, 20, 30, 40, 50};
for (int number : numbers) {
if (number == 30) {
continue;
}
cout << number << " ";
}
return 0;
}Output:
10 20 40 50Explanation:
The range-based loop processes each array element.
When the value is 30,
continueskips the print statement.The next element is processed.
All other values are printed.
7. Practical Example: Print Only Positive Numbers
Suppose you have an array containing positive, negative, and zero values. You want to display only positive numbers.
You can use continue to skip values that are zero or negative.
#include <iostream>
using namespace std;
int main() {
int numbers[] = {10, -5, 20, 0, -8, 30};
for (int number : numbers) {
if (number <= 0) {
continue;
}
cout << number << " ";
}
return 0;
}Output:
10 20 30Explanation:
The condition number <= 0 identifies zero and negative values. The continue statement skips those elements, so only positive numbers are displayed.
8. Using continue in Nested Loops
A nested loop means one loop is placed inside another loop.
When continue is used inside an inner loop, it skips the current iteration of that inner loop. The outer loop continues normally.
Example: Skip One Value in an Inner Loop
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 4; j++) {
if (j == 3) {
continue;
}
cout << i << " " << j << endl;
}
}
return 0;
}Output:
1 1
1 2
1 4
2 1
2 2
2 4
3 1
3 2
3 4Explanation:
The outer loop runs three times.
The inner loop runs from 1 to 4 during each outer iteration.
When
jbecomes 3,continueskips the current inner-loop iteration.The inner loop proceeds to 4.
The outer loop continues normally.
9. Using continue Inside a switch Nested in a Loop
A continue statement cannot be used in a standalone switch statement. However, it can be used inside a switch that is itself inside a loop.
In that situation, continue applies to the nearest enclosing loop, not the switch.
Example: Skip a Value Using switch
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) {
switch (i) {
case 3:
continue;
default:
cout << i << " ";
}
}
return 0;
}Output:
1 2 4 5Explanation:
The
forloop processes numbers from 1 to 5.The
switchchecks the current value.When
iis 3,continueskips the rest of the current loop iteration.The loop moves to the next value.
10. Difference Between break and continue in C++
Both break and continue are jump statements, but they control loops differently.
| Feature | break | continue |
|---|---|---|
| Purpose | Terminates the nearest enclosing loop or switch | Skips the remaining statements in the current loop iteration |
| Loop execution | Stops the loop | Continues with the next iteration |
| Used in for loop | Yes | Yes |
| Used in while loop | Yes | Yes |
| Used in do-while loop | Yes | Yes |
| Used in switch alone | Yes | No |
| Typical use | Stop searching after finding a value | Skip unwanted values |
Example Using break
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break;
}
cout << i << " ";
}
return 0;
}Output:
1 2The loop stops completely when i becomes 3.
Example Using continue
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
cout << i << " ";
}
return 0;
}Output:
1 2 4 5The loop skips 3 but continues processing the remaining values.
11. Common Mistakes When Using continue
Mistake 1: Forgetting to Update a while Loop Variable
Incorrect example:
int i = 1;
while (i <= 5) {
if (i == 3) {
continue;
}
cout << i << " ";
i++;
}Problem: When i becomes 3, the continue statement runs before i++. The value remains 3, so the condition stays true and the loop never ends.
Correct example:
int i = 1;
while (i <= 5) {
if (i == 3) {
i++;
continue;
}
cout << i << " ";
i++;
}Output:
1 2 4 5Mistake 2: Expecting continue to Exit the Loop
The continue statement does not terminate the loop. It only skips the current iteration.
Use break if you want to stop the loop completely.
Mistake 3: Using continue Outside a Loop
Incorrect example:
int main() {
int number = 10;
if (number == 10) {
continue;
}
return 0;
}This code produces a compilation error because continue is not inside a loop.
Mistake 4: Expecting continue to Skip an Entire switch Statement
The continue statement does not directly terminate a switch block. When a switch is inside a loop, continue skips the current iteration of that enclosing loop.
12. Best Practices for Using continue in C++
Follow these practices when using continue:
Use
continuewhen you want to skip unwanted values without stopping the loop.Keep the condition before the statements you want to skip.
In
whileloops, ensure the loop variable or other progress condition is updated before a possiblecontinue.In nested loops, remember that
continueapplies to the nearest enclosing loop.Avoid using too many
continuestatements if they make the program difficult to understand.Use meaningful conditions so other developers can easily understand why an iteration is skipped.
Test boundary values and conditions that might cause an infinite loop.
13. Frequently Asked Questions (FAQs)
Q1. What is the continue statement in C++?
The continue statement skips the remaining statements in the current iteration of a loop and moves execution towards the next iteration.
Q2. Can we use continue in a for loop?
Yes. You can use continue inside a for loop to skip selected iterations. The loop's update expression still executes before the next condition check.
Q3. Can we use continue in a while loop?
Yes. In a while loop, continue skips the remaining statements and moves control to the next condition check.
Q4. Can continue be used in a do-while loop?
Yes. In a do-while loop, continue skips the remaining statements and moves control to the condition check at the end of the loop.
Q5. Can we use continue in a switch statement?
No, not in a standalone switch statement. However, if the switch is inside a loop, continue can be used to continue the enclosing loop.
Q6. What is the difference between break and continue?
break terminates the nearest enclosing loop or switch, whereas continue skips the current loop iteration and proceeds with the next one.
Q7. Does continue work in nested loops?
Yes. It skips the current iteration of the nearest enclosing loop. Other enclosing loops continue normally.
Q8. Does continue increase the loop variable automatically?
Not always. In a traditional for loop, the update expression runs after continue. In while and do-while loops, you must ensure your code updates the loop variable where necessary.
Runnable Code Example: The continue Statement in C++
C++20 / C++23#include <iostream>
int main() {
std::cout << "Printing odd numbers between 1 and 10:
";
for (int i = 1; i <= 10; ++i) {
// Skip even numbers
if (i % 2 == 0) {
continue; // Skip the rest of the loop body
}
std::cout << i << " ";
}
std::cout << "
";
return 0;
}Common C++ Technical Interview Questions & Answers
Interview PrepQ: What is the key behavioral difference between continue in a for loop vs a while loop?
Key Takeaways, Modern Best Practices & Idioms
C++ Best Practices & Production Rules
continue as an early guard check at the top of loops to avoid deeply nested if statements.2. Ensure loop update variables in
while loops are incremented before invoking continue to avoid infinite loops.