switch statement in C

Consider a situation in which we have more number of options out of which we need to select only one option that is to be executed. Such kind of problems can be solved using nested if statement. But as the number of options increases, the complexity of the program also gets increased. This type of problems can be solved very easily using switch statement. Using switch statement, one can select only one option from more number of options very easily. In switch statement, we provide a value that is to be compared with a value associated with each option. Whenever the given value matches with the value associated with an option, the execution starts from that option. In switch statement every option is defined as a case.

The switch statement has the following syntax and execution flow diagram...

The switch statement contains one or more number of cases and each case has a value associated with it. At first switch statement compares the first case value with the switchValue, if it gets matched the execution starts from the first case. If it doesn't match the switch statement compares the second case value with the switchValue and if it is matched the execution starts from the second case. This process continues until it finds a match. If no case value matches with the switchValue specified in the switch statement, then a special case called default is executed.

When a case value matches with the switchValue, the execution starts from that particular case. This execution flow continues with next case statements also. To avoid this, we use "break" statement at the end of each case. That means the break statement is used to terminate the switch statement. However it is optional.

Example Program | Display pressed digit in words.

#include <stdio.h>
void main(){
   int n ;
   clrscr() ;
   printf("Enter any digit: ") ;
   scanf("%d", &n) ;
   switch( n )
   	case 0: printf("ZERO") ; 
        	break ;
   	case 1: printf("ONE") ; 
        	break ;
   	case 2: printf("TWO") ; 
        	break ;
   	case 3: printf("THREE") ; 
        	break ;
   	case 4: printf("FOUR") ; 
        	break ;
   	case 5: printf("FIVE") ; 
        	break ;
   	case 6: printf("SIX") ; 
        	break ;
   	case 7: printf("SEVEN") ; 
        	break ;
   	case 8: printf("EIGHT") ; 
        	break ;
   	case 9: printf("NINE") ; 
        	break ;
   	default: printf("Not a Digit") ;
   getch() ;
Output 1:

Enter any digit: 5

Output 2:

Enter any digit: 15
Not a Digit


When we use switch statement, we must follow the following...

  • Both switch and case are keywords so they must be used only in lower case letters.
  • The data type of case value and the value specified in switch statement must be same.
  • switch and case values must be either integer or character but not float or string.
  • A switch statement can contain any number of cases.
  • The keyword case and its value must be superated with a white space.
  • The case values need not be defined in sequence, they can be in any order.
  • The default case is optional and it can be defined anywhere inside the switch statement.
  • The switch value might be a direct value, a variable or an expression.