continue statement

From cppreference.com
< cpp‎ | language
 
 
C++ language
General topics
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (C++11)
Jump statements
continue - break
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until C++17*)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
decltype (C++11)
auto (C++11)
alignas (C++11)
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
Implicit conversions - Explicit conversions
static_cast - dynamic_cast
const_cast - reinterpret_cast
Memory allocation
Classes
Class-specific function properties
explicit (C++11)
static
Special member functions
Templates
Miscellaneous
 
Statements
Labels
label : statement
Expression statements
expression ;
Compound statements
{ statement... }
Selection statements
if
switch
Iteration statements
while
do-while
for
range for(C++11)
Jump statements
break
continue
return
goto
Declaration statements
declaration ;
Try blocks
try compound-statement handler-sequence
Transactional memory
synchronized, atomic_commit, etc(TM TS)
 

Causes the remaining portion of the enclosing for, range-for, while or do-while loop body to be skipped.

Used when it is otherwise awkward to ignore the remaining portion of the loop using conditional statements.

Syntax

attr (optional) continue ;

Explanation

The continue statement causes a jump, as if by goto to the end of the loop body (it may only appear within the loop body of for, range-for, while, and do-while loops).

More precisely,

For while loop, it acts as

while (/* ... */)
{
   // ...
   continue; // acts as goto contin;
   // ...
   contin:;
}

For do-while loop, it acts as:

do
{
    // ...
    continue; // acts as goto contin;
    // ...
    contin:;
} while (/* ... */);

For for and range-for loop, it acts as:

for (/* ... */)
{
    // ...
    continue; // acts as goto contin;
    // ...
    contin:;
}

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 << ") ";
        }
    }
}

Output:

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

See also