CKTIS / C++
continue — skip the rest of an iteration
The continue; statement skips the remainder of the current iteration of the innermost enclosing loop. It does not terminate the whole loop: subsequent control flow depends on the loop kind, including execution of the step expression in a traditional for loop.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
continue;continue; is a jump statement with no operand, terminated by a semicolon. In C++23 it must be inside a loop. It skips the remainder of that loop's current iteration but, unlike break, does not terminate the whole loop. In nested loops it affects only the innermost enclosing loop; it accepts no label to select an outer loop.
In a traditional for loop, continue leads to the step expression and then another condition check. In while and do, it leads to the condition check. In a range-based for, it leads to advancing the iterator and checking the end of the range. Another iteration is not guaranteed: the condition may end the loop.
Watch for a counter update below continue in a while body: it will be skipped, potentially causing an infinite loop. Leaving a scope through continue still destroys objects with automatic storage duration constructed in that scope, in reverse construction order.
The example skips even values of i, so sum receives 1 + 3 + 5, and the program prints 9 followed by a newline. ++i executes even after continue. This entry describes C++23; the supplied site hosts an evolving working draft, not a frozen C++23 publication.
Examples
#include <iostream>
int main() {
int sum = 0;
for (int i = 1; i <= 5; ++i) {
if (i % 2 == 0) {
continue;
}
sum += i;
}
std::cout << sum << '\n';
}
Result
9
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.