Namespaces
Variants
Views
Actions

switch statement

From cppreference.com
< cpp‎ | language
Revision as of 09:01, 10 May 2013 by P12 (Talk | contribs)

Transfers control to one of the several statements, depending on the value of a condition.

Contents

Syntax

Template:sparam(optional) switch ( Template:sparam ) Template:sparam
Template:sparam(C++11) - any number of attributes
Template:sparam - any expression of integral or enumeration type, or of a class type with a conversion function to integral or enumeration type, or a declaration of a single non-array variable of such type with a brace-or-equals initializer.
Template:sparam - any statement (typically a compound statement). case: and default: labels are permitted in Template:sparam and break; statement has special meaning.
Template:sparam(optional) case Template:sparam : Template:sparam (1)
Template:sparam(optional) default : Template:sparam (2)
Template:sparam - a constant expression of the same type as the type of Template:sparam after conversions and integral promotions

Explanation

The body of a switch statement may have an arbitrary number of case: labels, as long as the values of all Template:sparam are unique (after conversions/promotions).

If Template:sparam evaluates to the value that is equal to the value of one of Template:sparams, then control is transferred to the statement that is labeled with that Template:sparam.

If Template:sparam evaluates to the value that doesn't match any of the case: labels, and the default: label is present, control is trasferred to the statement labeled with the default: label.

The break statement, when encountered in Template:sparam exits the switch statement:

switch(1) {
    case 1 : cout << '1'; // prints "1",
    case 2 : cout << '2'; // then prints "2"
}
switch(1) {
    case 1 : cout << '1'; // prints "1"
             break;       // and exits the switch
    case 2 : cout << '2';
             break;
}

Because transfer of control is not permitted to enter the scope of a variable, if a declaration statement is encountered inside the Template:sparam, it has to be scoped in its own compound statement:

switch(1) {
    case 1: int x = 0; // initialization
            std::cout << x << '\n';
            break;
    default: // compilation error: jump to default: would enter the scope of 'x'
             // without initializing it
             std::cout << "default\n";
             break;
}
switch(1) {
    case 1: {  int x = 0;
               std::cout << x << '\n';
               break;
            } // scope of 'x' ends here
    default: std::cout << "default\n"; // no error
             break;
}

Keywords

switch, case, default

Example

The following code shows several usage cases of the switch statement

#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 is executed
    }
 
    std::cout << '\n';
 
    switch (i) {
        case 4: std::cout << "a";  //nothing is executed
    }
 
    // when enumerations are used in a switch statement, many compilers
    // issue warnings if one of the enumerators is not handled
    enum color {RED, GREEN, BLUE};
    switch(RED) {
        case RED:   std::cout << "red\n"; break;
        case GREEN: std::cout << "green\n"; break;
        case BLUE:  std::cout << "blue\n"; break;
    }
 
    // pathological examples
 
    // the statement doesn't have to be a compound statement
    switch(0)
        std::cout << "this does nothing\n";
 
    // labels don't require a compound statement either
    switch(int n = 1)
        case 0:
        case 1: std::cout << n << '\n';
 
    // Duff's Device: http://en.wikipedia.org/wiki/Duff's_device
}

Output:

2345
d
red
1