The switch Statement


Syntax

switch (expression)
 statement

Notes

The expression must be of integral type. If an instance of a class is specified for the expression then there must exist an unambiguous conversion from that class to integral type. Integral promotion is performed.

Any statement contained within the statement block may be labelled with one or more case labels. A case label consists of the keyword case, followed by a constant integral expression, followed by a colon. The constant expression is converted to match the type of the promoted switch expression. Within the switch statement, the labelled constant expressions must be unique. A default label may also be specified, which consists of the keyword default followed by a colon. If none of the case label expressions matches the switch expression then control is transferred to the code at the default label. If no default label exists and no case is matched, control is transferred to after the switch statement. At most one default label may exist within the body of a switch statement.

Example

switch (i)   // i is of type int
 {
  case 1:
   cout << "1 was encountered\n";
   break;
 
  case 2:
  case 3:
   cout << "2 or 3 was encountered\n";
   break;
 
  case 4:
   {
    int j=0;
    for (int i=0; i<10; i++) j += i;
     cout << "Case 4 was encountered and the answer is: " << j << "\n";
   }
   break;
 
  case 5:
   cout << "5 was encountered\n";
   cout << "Oops, forgot to break\n";
 
  case 6:
   cout << "6 was encountered\n";
   break;
    
  default:
   cout << "Default case was encountered\n";
 }

Notes

As may be observed in cases 2 and 3 above, multiple case statements may be attached to a single code point. Case 5 does not have a break statement after its code and before case 6; thus, program execution falls through to case 6 after the execution of the code associated with case 5 is complete. It may be inferred from this that the case statements themselves do not influence the path of execution when being passed through.

Case 4 illustrates another point. If an initialized variable is to be declared during the course of execution of a case then it should be placed within a nested scope (as achieved by the braces). Attempting to declare an explicitly or implicitly initialized variable with a scope matching the case labels causes the compiler to issue an error if there are cases that follow. This is because the matching of a case below the case containing the initialized variable causes the initialization to be bypassed. However, uninitialized variables may be declared with a scope matching the case labels.

The code within the body of the switch statement is executed in a linear fashion. That is, starting at the top of the statement, each case expression is evaluated and tested to see if it matches the value of the switch expression. If it does then execution begins at the code following that label, otherwise the next case is examined. This process continues until either the body of the switch statement is exhausted or control is transferred to a default label.

Switch statements may be nested; whereby, the case and default labels apply to the innermost containing switch statement.