Language[lang]
continue in C++: skip to the next loop iteration
- Section
- Language [lang]
- Standard
- C++17
- Compilers
- MSVC 19.44, Clang 19.1 (MSVC STL), GCC 16.2, Clang 23.1 (libc++)
- Published
- Length
- 7 min read
Abstract
continue in C++ skips the rest of the current loop iteration: a for loop then runs its increment and tests its condition, and a while or do-while loop tests its condition. It acts on the innermost loop only, passes through a switch to the loop around it, and does not compile outside a loop. In a while loop, update the loop variable before the continue, or the loop never ends.

continue in C++ ends the current iteration of the innermost loop that contains it: the rest of the loop body is skipped and the loop moves on. In a for loop the increment still runs; in a while or do-while loop control goes straight to the condition. Unlike break, it does not leave the loop.
The smallest example prints the even numbers from 1 to 10:
#include <iostream>
int main() {
for (int i = 1; i <= 10; ++i) {
if (i % 2 != 0) {
continue; // odd: skip the rest of this pass
}
std::cout << i << '\n'; // runs for even numbers only
}
}— end example]
It printed 2, 4, 6, 8 and 10, one per line. For each odd i, continue ended that pass (one run through the loop body) before the print; ++i still ran, so the loop went on to the next number. The % is the remainder operator (C++ operators lists the rest).
The samples are C++17, built with MSVC 19.44, Clang 19.1 (MSVC STL), GCC 16.2 and Clang 23.1 (libc++), and all four printed the same. continue has been in C++ since C++98 and needs no header; Since When lists what each standard added.
Where does control go after continue?
To the end of the loop body: the continue statement passes control to a point just past the body’s last statement ([stmt.cont]). From there the loop does what it does after every pass:
| Loop | After continue |
|---|---|
for (init; cond; inc) |
the increment, then the condition |
while (cond) |
the condition |
do { } while (cond); |
the condition, at the bottom |
for (x : range) |
the next element |
A for loop runs its increment before it tests the condition again ([stmt.for]), which is why the first example did not get stuck on 1. In a do-while loop the test is at the bottom, so continue jumps down to it: do { ++passes; continue; } while (false); ran its body once and stopped. The C++ do while loop covers that loop in full.

A range-based for loop, which makes one pass per element, moves on to the next element. Here it adds up sensor readings and skips the negative ones, which are bad readings:
std::vector<int> readings{12, -3, 7, -1, 9};
int sum = 0;
for (int r : readings) {
if (r < 0) {
continue; // a bad reading: on to the next one
}
sum += r;
}
std::cout << "sum: " << sum << '\n';— end example]
It printed sum: 28: 12, 7 and 9 were added, -3 and -1 skipped.
Why does continue make my while loop run forever?
Because the continue skips the line that moves the loop on. In a while loop the increment is an ordinary statement of the body; when a continue comes before it, the variable never changes, the condition stays true, and the loop repeats the same pass forever. This is the bug as it is usually written:
void ship(const std::vector<int>& quantities) {
std::size_t i = 0;
while (i < quantities.size()) {
int q = quantities[i];
if (q <= 0) {
continue; // jumps over ++i: i never changes
}
std::cout << "ship " << q << '\n';
++i;
}
}— end example]
Given the quantities {3, 0, 5, -2, 4}, it ships 3 and then sits on the 0 for good. With a counter added to stop it, the same loop printed ship 3 and then stuck: pass 6, still on quantity 0: five values need five passes, so a sixth means i stopped moving. None of the four compilers warned, at /W4 or -Wall -Wextra, optimized or not, which is why this bug survives until the program runs.
The fix is to move on first and decide to skip after:
void ship_fixed(const std::vector<int>& quantities) {
std::size_t i = 0;
while (i < quantities.size()) {
int q = quantities[i];
++i; // move on first ...
if (q <= 0) {
continue; // ... so skipping is safe
}
std::cout << "ship " << q << '\n';
}
}— end example]
It printed ship 3, ship 5 and ship 4. A for loop avoids the bug by design, since its increment sits in the header and runs after every continue. The C++ while loop covers the other ways a while loop fails to stop.
continue vs break: what is the difference?
break leaves the loop (the break statement on cppreference); continue leaves only the current pass, and the loop goes on.
| Behavior | continue |
break |
|---|---|---|
| Ends | the current pass | the whole loop |
| Control goes to | the increment or the condition | the statement after the loop |
| Allowed in | loops only | loops and switch |
One loop with both:
#include <iostream>
int main() {
for (int i = 1; i <= 10; ++i) {
if (i % 3 == 0) {
continue; // 3, 6: skip the print, keep looping
}
if (i == 8) {
break; // 8: leave the loop for good
}
std::cout << i << '\n';
}
std::cout << "after the loop\n";
}— end example]
It printed 1, 2, 4, 5 and 7, then after the loop. continue skipped 3 and 6 and the loop carried on; break at 8 ended it, so 9 and 10 never came up.
What does continue do in nested loops?
It continues the innermost loop that contains it; the outer loop is not touched:
#include <iostream>
int main() {
for (int row = 1; row <= 3; ++row) {
for (int col = 1; col <= 3; ++col) {
if (col == 2) {
continue; // the inner loop's next column
}
std::cout << "row " << row << ", column " << col << '\n';
}
std::cout << "end of row " << row << '\n';
}
}— end example]
Each row printed columns 1 and 3 and then its end of row line: the continue skipped column 2 in the inner loop, and the outer loop ran all three rows. The same rule is why a continue inside a lambda written in a loop body does not compile: the lambda’s body is a function of its own, with no loop in it. A return there ends that call of the lambda.
C++ has no labelled continue (Java has one) to skip the rest of an outer pass from inside the inner loop. To get that effect, set a flag, break out of the inner loop and continue the outer one when the flag is set. Or move the inner loop into a function of its own that returns whether to skip, and continue on that; or goto a label at the end of the outer loop’s body.
Can you use continue inside a switch?
Yes, when the switch is inside a loop. A switch is not a loop, so continue passes through it and starts the loop’s next pass, while break leaves only the switch:
#include <iostream>
#include <string>
int main() {
std::string commands = "asa";
for (char c : commands) {
switch (c) {
case 'a':
std::cout << c << ": add\n";
break; // leaves the switch only
case 's':
std::cout << c << ": skip\n";
continue; // leaves the switch and this pass
}
std::cout << c << ": after the switch\n";
}
}— end example]
It printed:
a: add
a: after the switch
s: skip
a: add
a: after the switch— end example]
After break, the line below the switch ran; after continue, it did not. With no loop around it, continue does not compile on any of the four, inside a switch or not: GCC says error: continue statement not within a loop, and the others word it differently.
When should you use continue?
To drop the cases you do not want at the top of the loop body, as a guard, so the real work is not nested inside an if. The readings loop above does it with continue; the same loop with an if printed total: 28, the same sum:
int total = 0;
for (int r : readings) {
if (r >= 0) { // the same loop without continue
total += r;
}
}
std::cout << "total: " << total << '\n';— end example]
With one short condition and a one-line body, as here, the if reads better: what runs is in plain sight. continue earns its place when there are several cases to skip or the body is long: each guard sits on its own line at the top, and the work below stays at one level of indentation.
Before you write one, check:
- In a
whileloop, the loop variable changes before anycontinue. - In a
switchinside a loop,breakleaves theswitchandcontinuegoes to the loop. - To skip the rest of an outer pass, leave the inner loop with
breakorreturn, thencontinuethe outer one, or usegoto. - In a lambda,
continuedoes not compile;return;ends the lambda’s call, not the loop’s pass.
More answers about the core language are in Language.
Questions
What does continue do in C++?
It ends the current iteration of the innermost loop around it: the rest of the loop body is skipped and the loop moves on. A for loop runs its increment next, then tests its condition; a while or do-while loop tests its condition.
What is the difference between break and continue in C++?
break leaves the loop, and the program goes on after it; continue skips only the rest of the current pass, and the loop keeps going. break is also allowed in a switch, where it leaves the switch; continue is allowed only inside a loop.
Does continue work in a while loop?
Yes. It skips the rest of the body and goes to the condition. Update the loop variable before the continue: an increment placed after it is skipped as well, the condition never changes, and the loop repeats the same pass forever.
Can I use continue in a switch statement?
Only when the switch is inside a loop. There, continue leaves the switch and starts the loop’s next pass, while break leaves only the switch. With no loop around it, continue does not compile.
Does continue skip the increment in a for loop?
No. After a continue, a for loop runs its increment, the third part of its header, and then tests the condition, as it does at the end of every pass.
Can continue skip to the next pass of an outer loop?
No. It acts on the innermost loop only, and C++ has no labelled continue. Set a flag and break out of the inner loop, then continue the outer loop when the flag is set, or use goto to jump to a label at the end of the outer loop’s body.
Sources
- continue statementcppreference.com
- break statementcppreference.com
- [stmt.cont] The continue statementC++ standard working draft
- [stmt.for] The for statementC++ standard working draft
- continue Statement (C++)Microsoft Learn