◐ Shell
clean mode source ↗

switch statement — cppreference.com

De cppreference.com

<metanoindex/>

Exécute le code en fonction de la valeur d'un argument intégrante

Original:

Executes code according to value of an integral argument

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

Lorsqu'une ou plusieurs de nombreuses branches de code doivent être exécutées selon une valeur intégrale .

Original:

Used where one or several out of many branches of code need to be executed according to an integral value.

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

Syntaxe

switch ( expression ) {
case constant_expression1 :
statement1 (en option)
case constant_expression2 :
statement2 (en option)
... ... ...
case constant_expressionn :
statementn (en option)
default: default_statement (en option)

}

Explication

expression doit être une expression, convertible en une valeur entière .

Original:

expression shall be an expression, convertible to an integer value.

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

Tous les constant_expressions doit être des expressions constantes, convertibles en un nombre entier, ce qui est unique dans cette déclaration switch

Original:

All constant_expressions shall be constant expressions, convertible to an integer value, which is unique within this switch statement

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

Si la expression prend une valeur égale à la valeur de l'une des constant_expressioni défini, le statementi (s'il est présent) et tous les états ultérieurs (sauf default_statement, si elle est présente) sont exécutées. Si la valeur de la expression ne correspond à aucune des constant_expressions, le default_statement est exécutée si présent .

Original:

If the expression evaluates to a value, equal to the value of one of the defined constant_expressioni, the statementi (if present) and all subsequent statements (except default_statement, if present) are executed. If the value of the expression does not match any of the constant_expressions, the default_statement is executed if present.

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

Il est utile de noter que, si l'exécution des instructions suivantes est indésirable, l'

instruction break

Original:

break statement

The text has been machine-translated via [http://translate.google.com Google Translate].
You can help to correct and verify the translation. Click [http://en.cppreference.com/w/Cppreference:MachineTranslations here] for instructions.

peuvent être utilisés. Dans ce cas, l'exécution de l'instruction se termine switch .

Original:

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

switch, case, default

Exemple

Le code suivant illustre l'utilisation de plusieurs cas de l'interrupteur' déclaration

Original:

The following code shows several usage cases of the switch statement

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

#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';
    
    switch (i) {
        case 4: std::cout << "a";
        default: std::cout << "d"; //there are no applicable constant_expressions 
                                   //therefore default_statement is executed
    }

    std::cout << '\n';

    switch (i) {
        case 4: std::cout << "a";  //nothing is executed
    }
}

Résultat :