Namespaces
Variants
Views
Actions

switch statement

From cppreference.com
< cpp‎ | language
Revision as of 19:36, 9 April 2013 by Dieram3 (Talk | contribs)

 
 
C++ language
General topics
Flow control
Conditional execution statements
switch statement
Iteration statements
Jump statements
Functions
function declaration
lambda function declaration
function template
inline specifier
exception specifications (deprecated)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
decltype specifier (C++11)
Specifiers
cv specifiers
storage duration specifiers
constexpr specifier (C++11)
auto specifier (C++11)
alignas specifier (C++11)
Initialization
Literals
Expressions
alternative representations
Utilities
Types
typedef declaration
type alias declaration (C++11)
attributes (C++11)
Casts
implicit conversions
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-style and functional cast
Memory allocation
Classes
Class-specific function properties
Special member functions
Templates
class template
function template
template specialization
parameter packs (C++11)
Miscellaneous
Inline assembly
 

Executes code according to value of an integral argument

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

Contents

Syntax

switch ( Template:sparam ) {
case Template:sparam :
Template:sparam (optional)
case Template:sparam :
Template:sparam (optional)
... ... ...
case Template:sparam :
Template:sparam (optional)
default: Template:sparam (optional)

}

Explanation

Template:sparam shall be an expression, convertible to an integer value.

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

If the Template:sparam evaluates to a value, equal to the value of one of the defined Template:sparam, the Template:sparam (if present) and all subsequent statements (including Template:sparam, if present) are executed. If the value of the Template:sparam does not match any of the Template:sparams, the Template:sparam is executed if present.

It is useful to note, that if the execution of subsequent statements is undesirable, the break statement can be used. In that case the execution of the switch statement terminates.

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_statement is executed
    }
 
    std::cout << '\n';
 
    switch (i) {
        case 4: std::cout << "a";  //nothing is executed
    }
}

Output:

2345
d