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

Veranlasst, dass der verbleibende Teil des Rumpfes der umschließenden for -, range-for -, while - oder do-while -Schleife übersprungen wird.

Wird verwendet, wenn es anderweitig umständlich wäre, den verbleibenden Teil der Schleife mit bedingten Anweisungen zu überspringen.

Inhaltsverzeichnis

Syntax

attr  (optional) continue ;

Erklärung

Die continue -Anweisung bewirkt einen Sprung, als ob durch goto , zum Ende des Schleifenkörpers (sie darf nur im Schleifenkörper von for -, range-for -, while - und do-while -Schleifen erscheinen).

Genauer gesagt,

Für die while -Schleife fungiert es als

while (/* ... */)
{
   // ...
   continue; // wirkt wie goto contin;
   // ...
   contin:;
}

Für die do-while Schleife wirkt es wie folgt:

do
{
    // ...
    continue; // wirkt wie goto contin;
    // ...
    contin:;
} while (/* ... */);

Für for und range-for Schleifen wirkt es wie folgt:

for (/* ... */)
{
    // ...
    continue; // wirkt wie goto contin;
    // ...
    contin:;
}

Schlüsselwörter

continue

Beispiel

#include <iostream>
int main()
{
    for (int i = 0; i < 10; ++i)
    {
        if (i != 5)
            continue;
        std::cout << i << ' ';      // diese Anweisung wird übersprungen, wenn i != 5
    }
    std::cout << '\n';
    for (int j = 0; 2 != j; ++j)
        for (int k = 0; k < 5; ++k) // nur diese Schleife wird von continue beeinflusst
        {
            if (k == 3)
                continue;
            // diese Anweisung wird übersprungen, wenn k == 3:
            std::cout << '(' << j << ',' << k << ") ";
        }
    std::cout << '\n';
}

Ausgabe:

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

Siehe auch

C-Dokumentation für continue