Namespaces
Variants

continue statement

From cppreference.net
C++ language
General topics
Flow control
Conditional execution statements
Iteration statements (loops)
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
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
Special member functions
Templates
Miscellaneous

Вызывает пропуск оставшейся части тела объемлющего цикла for , range-for , while или do-while .

Используется, когда неудобно игнорировать оставшуюся часть цикла с помощью условных операторов.

Содержание

Синтаксис

attr  (необязательно) continue ;

Объяснение

Инструкция continue вызывает переход, как если бы с помощью goto в конец тела цикла (она может появляться только в теле циклов for , range-for , while и do-while ).

Более точно,

Для цикла while он действует как

while (/* ... */)
{
   // ...
   continue; // действует как goto contin;
   // ...
   contin:;
}

Для цикла do-while он действует как:

do
{
    // ...
    continue; // действует как goto contin;
    // ...
    contin:;
} while (/* ... */);

Для цикла for и range-for он действует следующим образом:

for (/* ... */)
{
    // ...
    continue; // действует как goto contin;
    // ...
    contin:;
}

Ключевые слова

continue

Пример

#include <iostream>
int main()
{
    for (int i = 0; i < 10; ++i)
    {
        if (i != 5)
            continue;
        std::cout << i << ' ';      // этот оператор пропускается каждый раз, когда i != 5
    }
    std::cout << '\n';
    for (int j = 0; 2 != j; ++j)
        for (int k = 0; k < 5; ++k) // только этот цикл затрагивается оператором continue
        {
            if (k == 3)
                continue;
            // этот оператор пропускается каждый раз, когда k == 3:
            std::cout << '(' << j << ',' << k << ") ";
        }
    std::cout << '\n';
}

Вывод:

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

Смотрите также