Friday, July 29, 2016

C Basics - Switch Statement in C

The special control statement that allows us to make a decision from the number of choices is called a switch. A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case.

The syntax for a switch statement is,

switch(expression) {

   case constant-expression  :
      statement(s);
      break; /* optional */
 
   case constant-expression  :
      statement(s);
      break; /* optional */
  
   /* you can have any number of case statements */
   default : /* Optional */
   statement(s);
}

The expression used in a switch statement must have an integral or enumerated type. The integer expression following the keyword switch is any C expression that will yield an integer value. It could be an integer constant like 1, 2 or 3, or an expression that evaluates to an integer. The keyword case is followed by an integer or a character constant. Each constant in each case must be different from all the others.

First, the integer expression following the keyword switch is evaluated. The value it gives is then matched, one by one, against the constant values that follow the case statements. When a match is found, the program executes the statements following that case. The break statement in a switch takes the control outside the switch. If no break appears, the flow of control will fall through to subsequent cases until a break is reached. If no match is found with any of the case statements, only the statements following the default are executed.

If we have no default case, then the program simply falls through the entire switch and continues with the next instruction (if any,) that follows the closing brace of switch.

All that we can have after the case is an int constant or a char constant or an expression that evaluates to one of these constants. Even a float is not allowed.
  • A float expression cannot be tested using a switch.
  • Cases can never have variable expressions.
  • Multiple cases cannot use same expressions.
switch works faster than an equivalent if-else ladder. the compiler generates a jump table for a switch during compilation. As a result, during execution it simply refers the jump table to decide which case should be executed, rather than actually checking which case is satisfied. A switch with 10 cases would work faster than an equivalent if-else ladder. Also, a switch with 2 cases would work slower than if-else ladder.

/* Demonstration of switch statement */
#include <stdio.h>
 
int main () {

   /* local variable definition */
   int month;
   printf ( "\nEnter a month number(1 - 12): " ) ; 
   scanf ( "%d", &month) ;

   switch(month) {
      case 1 :
         printf("Your month is January\n" );
         break;
     case 2 :
         printf("Your month is February\n" );
         break;
     case 3 :
         printf("Your month is March\n" );
         break;   
     case 4 :
         printf("Your month is April\n" );
         break;   
     case 5 :
         printf("Your month is May\n" );
         break;   
     case 6 :
         printf("Your month is June\n" );
         break;   
     case 7 :
         printf("Your month is July\n" );
         break;   
     case 8 :
         printf("Your month is August\n" );
         break;
     case 9 :
         printf("Your month is September\n" );
         break;
     case 10 :
         printf("Your month is October\n" );
         break;
     case 11 :
         printf("Your month is November\n" );
         break;
     case 12 :
         printf("Your month is December\n" );
         break;
      default :
         printf("Invalid month number\n" );
   }
 
   return 0;
}
Output of above program,

Enter a month number(1 - 12): 6
Your month is June

The nested switch statement:
It is possible to have a switch as a part of the statement sequence of an outer switch. Even if the case constants of the inner and outer switch contain common values, no conflicts will arise.

/* Demonstration of nested switch statement */
#include <stdio.h>
 
int main () {

   /* local variable definition */
   int num1 = 1;
   int num2 = 2;

   switch(num1) {
      case 1 :
  printf("This is case 1 in outer switch \n" );
  switch(num2) {
     case 2:
            printf("This is case 2 in inner switch\n" );
     break;
     case 1:
            printf("This is case 1 in inner switch \n" );
     break;
  }
         break;
     case 2 :
         printf("This is case 2 in outer switch \n" );
         break;
     default :
         printf("Invalid input\n" );
   }
 
   return 0;
}
Output of above program,

This is case 1 in outer switch
This is case 2 in inner switch



Related topics:
Overview of Statements in C   |   Decision Making Statements in C   |   The if Statement in C   |   The if-else Statement in C   |   The else-if Statement in C   |   Nested if-else Statement in C   |   Forms of if Statement in C   |   Conditional Operator Statement in C   |   The null Statement in C

List of topics: C Programming

No comments:

Post a Comment