Namespaces
Variants

continue statement

From cppreference.net

Veranlasst, dass der verbleibende Teil des Rumpfes der umschließenden 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-spec-seq (optional) continue ;
attr-spec-seq - (C23) optionale Liste von Attributen , angewendet auf die continue -Anweisung

Erklärung

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

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 die for -Schleife gilt:

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

Schlüsselwörter

continue

Beispiel

#include <stdio.h>
int main(void) 
{
    for (int i = 0; i < 10; i++) {
        if (i != 5) continue;
        printf("%d ", i);             // this statement is skipped each time i != 5
    }
    printf("\n");
    for (int j = 0; j < 2; j++) {
        for (int k = 0; k < 5; k++) { // only this loop is affected by continue
            if (k == 3) continue;
            printf("%d%d ", j, k);    // this statement is skipped each time k == 3
        }
    }
}

Ausgabe:

5
00 01 02 04 10 11 12 14

Referenzen

  • C17-Standard (ISO/IEC 9899:2018):
  • 6.8.6.2 Die continue-Anweisung (S: 111)
  • C11-Standard (ISO/IEC 9899:2011):
  • 6.8.6.2 Die continue-Anweisung (S: 153)
  • C99-Standard (ISO/IEC 9899:1999):
  • 6.8.6.2 Die continue-Anweisung (S: 138)
  • C89/C90 Standard (ISO/IEC 9899:1990):
  • 3.6.6.2 Die continue-Anweisung

Siehe auch

C++ Dokumentation für continue Anweisung