The perfect place for easy learning...

C Programming Language


Topics List

Place your ad here

'switch' statement in C

Consider a situation in which we have many 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 problem can be solved very easily using a switch statement. Using the switch statement, one can select only one option from more number of options very easily. In the switch statement, we provide a value that is to be compared with a value associated with each option. Whenever the given value matches the value associated with an option, the execution starts from that option. In the 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 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 the next case statements also. To avoid this, we use the "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.


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:

switch in c programming

Output 2:

switch in c programming


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 the switch statement must be the 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 direct, a variable or an expression.

Place your ad here
Place your ad here