Abstract

The switch statement provides a multiway selection control structure to execute a selected section of code determined by the run-time value of an expression.  The condition is an expression which evaluates to an integer value, which include signed, unsiened, char, enum, and even boolean values.

Selection statements are also referred to as conditional statements.  Other C selection statements are the if statement and the if-else statement.

Usage

The switch statement is used to provide a multiway selection, based on an integral value.

Syntax

Syntax Diagrams

switch Statement Syntax Diagram case Syntax Diagram

BNF

switch-statement
::= 'switch' '(' <dynamic-expression> ')' '{' <declaration-list> <statement-list> <case-list> '}'
case-list
::= <case>
::= <case-list> <case>
case
::= <case-label-list> <statement-list>
case-label
::= 'case' <static-expression> ':'
::= 'default' ':'
dynamic-expression
::= <expression>
static-expression
::= <expression>
statement-list
::= <empty>
::= <statement>
::= <statement-list> <statement>
declaration-list
::= <declaration-list> <declaration>
::= <declaration>
::= <empty>

EBNF

switch-statement
::= 'switch' '(' <dynamic-expression> ')' '{' <declaration> * <statement> * <case> * '}'
case
::= <case-label> * <statement> *
case-label
::= 'case' <static-expression> ':'
::= 'default' ':'
dynamic-expression
::= <expression>
static-expression
::= <expression>

Contextual Constraints

expression
The controlling expression of a switch statement must have an integral value.
local-declaration
While C syntax allows local declarations at the beginning of the switch body to have initializers, these initializers are never applied, and have no meaning.
executable-statement
While C syntax allows executable statements before the first case, they are unreachable, are never executed, and have no meaning.
static-expression
The static expression of a case is evaluated statically at compile time, and must have an integral value.
The static expressions of the cases cannot have duplicate values.
The default case can appear at most once is a switch statement.

Semantics

switch-statement
The controlling expression is evaluated.
The value of the controlling expression is compared to the case values.
If the value of the controlling expression is equal to a case value, control is transferred to that case.
If the value of the controlling expression is not equal to any case value, control is transferred to the default case, if there is one.
If the value of the controlling expression is not equal to any case value, control is transferred to the point following the switch statement, if there is no default case.

Flowchart

Flowchart of switch statement

Remarks

Example

#include <stdio.h>

int main () {
  int Count;
  
  Count = 2;
  switch (Count) {
    case 0:
      printf ("None.\n");
      break;
    case 1:
      printf ("Only one.\n");
      break;
    case 2:
      printf ("A pair.\n");
      break;
    case 3:
      printf ("Three.\n");
      break;
    default:
      printf ("Many.\n");
      break;
  }
  
  return 0;
}

Output


    

Reference Links

Break Statement <break-statement>
Statement <statement>
Expression <expression>

Other Languages

C Now
C Switch Statement
C++ Now C++ Switch Statement
Java Now Java Switch Statement
Pascal Now Pascal Case Statement
PHP Now PHP Switch Statement