Namespaces
Variants

continue statement

From cppreference.com
 
 
C++ language
General topics
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (C++11)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until C++17*)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
const/volatile
decltype (C++11)
auto (C++11)
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Memory allocation
Classes
Class-specific function properties
explicit (C++11)
static

Special member functions
Templates
Miscellaneous
 
 

Terminates the current iteration.

Syntax

attr (optional) continue ;
attr - (since C++11) any number of attributes

Explanation

continue statements must be enclosed by any of the following statements:

(since C++26)

For iteration statements

When executing a continue statement, if the innermost such enclosing statement is an iteration statement, then control passes to the end of the iteration element's statement (i.e. the loop body).

while (/* ... */)
{
    /* executed statements */
    continue;
    /* skipped statements */
}

do
{
    /* executed statements */
    continue;
    /* skipped statements */
} while (/* ... */)

for (/* ... */)
{
    /* executed statements */
    continue;
    /* skipped statements */
}

For expansion statements

When executing a continue statement, if the innermost such enclosing statement is an expansion statement, then control passes to the end of the expansion element's current expansion item's compound-statement.

template for (/* ... */)
{
    if (/* current item is the Nth */)
        continue;
    /* statements only skipped for the Nth item */
}
(since C++26)

Keywords

continue

Example

#include <iostream>

int main()
{
    for (int i = 0; i < 10; ++i)
    {
        if (i != 5)
            continue;
        std::cout << i << ' ';      // this statement is skipped each time i != 5
    }
    std::cout << '\n';
    
    for (int j = 0; 2 != j; ++j)
        for (int k = 0; k < 5; ++k) // only this loop is affected by continue
        {
            if (k == 3)
                continue;
            // this statement is skipped each time k == 3:
            std::cout << '(' << j << ',' << k << ") ";
        }
    std::cout << '\n';
}

Output:

5
(0,0) (0,1) (0,2) (0,4) (1,0) (1,1) (1,2) (1,4)

See also

C documentation for continue