◐ Shell
clean mode source ↗

break statement — cppreference.com

De cppreference.com

<metanoindex/>

Utilisé quand il en est autrement difficile à terminer la boucle à l'aide de l'expression de la condition et des instructions conditionnelles .

Original:

Used when it is otherwise awkward to terminate the loop using the condition expression and conditional statements.

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Syntaxe

break

Explication

Après cette déclaration, le contrôle est transféré à l'instruction qui suit immédiatement la boucle englobante. Tous les objets de stockage automatiques déclarés à enfermer la boucle sont détruits avant l'exécution de la première ligne qui suit la boucle entourant .

Original:

After this statement the control is transferred to the statement immediately following the enclosing loop. All automatic storage objects declared in enclosing loop are destroyed before the execution of the first line following the enclosing loop.

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Mots-clés

break

Exemple

#include <iostream>

int main()
{
    int i = 2;
    switch (i) {
        case 1: std::cout << "1";
        case 2: std::cout << "2";   //execution starts at this case label
        case 3: std::cout << "3";
        case 4:
        case 5: std::cout << "45";
                break;              //execution of subsequent statements is terminated
        case 6: std::cout << "6";
    }

    std::cout << '\n';
 
    for (int j = 0; j < 2; j++) {
        for (int k = 0; k < 5; k++) {         //only this loop is affected by break
            if (k == 2) break;
            std::cout << j << k << " ";
        }
    }
}

Résultat :