In C++, the continue statement skips the remaining statements in the current loop iteration and moves control toward the next iteration. In this tutorial, you will learn how the C++ continue statement behaves in while, do-while, for, and nested loops, with example programs.
How the C++ Continue Statement Works
When a continue statement is executed, C++ does not terminate the loop. It skips the statements that remain in the current iteration and proceeds according to the normal iteration mechanism of that loop.
- In a
forloop, control proceeds to the loop’s update expression and then the condition is checked again. - In a
whileloop, control proceeds to the loop condition. - In a
do-whileloop, control proceeds to the condition at the bottom of the loop.
This makes continue useful when one iteration should be ignored without stopping the entire loop.
C++ Continue Statement Syntax
The syntax of the C++ continue statement is:
continue;
The continue statement must be associated with an enclosing iteration statement such as for, while, or do-while. It may appear inside an if statement when that if statement itself is inside a loop.
C++ Continue Statement in a While Loop
In a while loop, executing continue skips the rest of the loop body and returns control to the loop condition. Therefore, any update that must happen before the next condition check needs special attention.
In the following example, the loop attempts to print the numbers from 0 through 9. When i is 4, the program increments i and executes continue. The cout statement is skipped for that iteration, so 4 does not appear in the output.
Refer C++ While Loop tutorial.
main.cpp
#include <iostream>
using namespace std;
int main() {
int i=0;
while (i < 10) {
if (i==4) {
i++;
continue;
}
cout << i << " ";
i++;
}
}
Output
0 1 2 3 5 6 7 8 9
The position of i++ inside the if block is important. If the program executed continue while i remained 4, control would return to the while condition with the same value. The condition would still be true, the same branch would run again, and the loop could continue indefinitely.
C++ Continue Statement in a Do-While Loop
In a do-while loop, continue skips the remaining statements in the body and proceeds to the condition at the bottom of the loop. This behavior is important because the loop condition may itself update the control variable.
In the following example, the loop prints numbers from 0 through 9 except 5. When i becomes 5, continue skips the cout statement. Control then reaches the while (++i < 10) condition, where i is incremented before the next iteration.
Refer C++ Do-While Loop tutorial.
main.cpp
#include <iostream>
using namespace std;
int main() {
int i=0;
do {
if (i==5) {
continue;
}
cout << i << " ";
} while (++i < 10);
}
Output
0 1 2 3 4 6 7 8 9
C++ Continue Statement in a For Loop
A for loop has a convenient interaction with continue. When continue executes, the remaining statements in the body are skipped, but the loop’s update expression still runs before the condition is tested for the next iteration.
In the following example, the loop prints values from 0 through 9. When i becomes 7, continue skips the output statement. The update expression i++ then executes normally, so the loop proceeds with i equal to 8.
Refer C++ For Loop tutorial.
main.cpp
#include <iostream>
using namespace std;
int main() {
for (int i=0; i < 10; i++) {
if (i==7) {
continue;
}
cout << i << " ";
}
}
Output
0 1 2 3 4 5 6 8 9
Using C++ Continue with an If Condition
A common pattern is to place continue inside an if statement that is itself inside a loop. The condition decides which iterations should be skipped.
For example, the following program prints only odd numbers. Whenever the current number is even, continue skips the output statement.
#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 9
Here, the if statement does not make continue valid by itself. The statement is valid because both the if block and continue are enclosed by the for loop.
C++ Continue Statement in Nested Loops
In nested loops, an ordinary continue statement applies to the nearest enclosing loop. It does not automatically skip an iteration of every surrounding loop.
In the following example, continue belongs to the inner while loop. When j > i, it advances the inner-loop control variable and starts the next inner-loop iteration. The outer loop continues independently.
main.cpp
#include <iostream>
using namespace std;
int main() {
int i = 1;
while (i <= 5) {
int j = 1;
while (j <= 5) {
if(j>i) {
j++;
continue;
}
cout << " \*";
j++;
}
cout << "\
";
i++;
}
}
Output
\*
\* \*
\* \* \*
\* \* \* \*
\* \* \* \* \*
C++ Continue vs Break
continue and break both change the normal flow of a loop, but they serve different purposes.
| Statement | What happens | Does the loop continue? |
|---|---|---|
continue | Skips the remaining statements in the current iteration | Yes, execution proceeds toward the next iteration |
break | Terminates the enclosing loop immediately | No, execution continues after the loop |
Use continue when only certain iterations should be ignored. Use break when there is no reason to execute any more iterations of the loop.
Why Continue Cannot Be Used in a Standalone If-Else Statement
An if statement alone does not create an iteration. Therefore, a continue statement inside an if or else block is valid only when that block is also enclosed by a loop.
In the following example, we shall write a continue statement inside C++ If Else statement, with no loop surrounding the continue statement.
mian.cpp
#include <iostream>
using namespace std;
int main() {
int n = 20;
if (n%2 == 0) {
if(n%10 == 0) {
continue;
}
cout << "something even" << endl;
} else {
cout << "odd" << endl;
}
}
Output
d:\workspace\cpp\main.cpp: In function 'int main()':
d:\workspace\cpp\main.cpp:8:10: error: continue statement not within a loop
8 | continue;
| ^~~~~~~~
The terminal process terminated with exit code: 1
The compiler reports an error because there is no enclosing loop whose next iteration could be continued.
Common Mistakes with the C++ Continue Statement
- Forgetting to update a while-loop variable: if the update is below
continue, it may never execute for the skipped case and the loop can become infinite. - Expecting continue to end the loop:
continueskips one iteration;breakexits the loop. - Using continue outside a loop: putting it in a standalone
ifstatement causes a compilation error. - Assuming continue affects an outer loop: in nested loops, it acts on the nearest enclosing loop.
- Putting required work after continue: statements below an executed
continueare not run during that iteration.
C++ Continue Statement Execution Rules
| Loop type | Where control goes after continue | What to watch for |
|---|---|---|
for | Update expression, then condition check | The update expression still executes |
while | Condition check | Update loop variables before continue when necessary |
do-while | Condition at the bottom of the loop | The condition is still evaluated after continue |
| Nested loop | Next iteration of the nearest enclosing loop | Outer loops are not directly continued |
C++ Continue Statement Summary
The C++ continue statement skips the rest of the current loop iteration without terminating the loop. In a for loop, execution proceeds through the update expression; in a while loop, it returns to the condition; and in a do-while loop, it proceeds to the condition at the bottom. In nested loops, continue affects the nearest enclosing loop.
In this C++ Tutorial, we learned how to use continue to skip selected iterations and how its behavior differs across C++ loop statements.
C++ Continue Tutorial Editorial QA Checklist
- Confirm that every explanation states that
continueskips the current iteration rather than terminating the loop. - Verify that the
forloop explanation mentions execution of the update expression aftercontinue. - Verify that the
whileloop example explains whyiis incremented beforecontinue. - Confirm that the
do-whileexplanation correctly sends control to the bottom condition. - Check that nested-loop guidance identifies the nearest enclosing loop as the loop affected by
continue. - Keep the distinction between
continueandbreakexplicit: skip one iteration versus terminate the loop. - Confirm that examples using
continueinsideifdistinguish a loop-enclosediffrom a standaloneif.
TutorialKart.com