Learn C - C switch






switch..case can be declared as below:

   switch(option){ 
      case option1: 
              // do option1 job 
              break; 
      case option2: 
              // do option2 job 
              break; 
   } 

The switch statement enables you to choose one action from a list of actions, based on the result of an integer expression.

The general syntax for the switch statement is as follows:

switch(integer_expression)
{
  case constant_value_1:
    statements_1;
    break;
    ....
  case constant_value_n:
    statements_n;
    break;
  default:
    statements;
    break;
}

If the integer_expression corresponds to one of the case values defined by the associated constant_value_n values, the statements following that case value are executed.

If the value of integer_expression differs from every one of the case values, the statements following default are executed.

You can omit the default keyword and its associated statements.

Example

Here is the sample code of switch..case usage:


   #include <stdio.h> 
//from  ww  w . j  av a 2  s  .  c o  m
   int main() { 
      // you can obtain input value from keyboard 
      // or any input device 
      int input = 3; 

      switch(input){ 
      case 1: 
              printf("choosen 1\n"); 
              break; 
      case 2: 
              printf("choosen 2\n"); 
              break; 
      case 3: 
      case 4 : 
              printf("choosen 3\n"); 
              break; 
      } 
      return 0; 
   } 

The code above generates the following result.





Example 2

Example


    #include <stdio.h>
//from   w  w w  .  j  a v  a 2  s  . c  o  m
    int main(void)
    {
      int choice = 0;            // The number chosen

      // Get the choice input
      printf("Pick a number between 1 and 10! ");
      scanf("%d", &choice);

      // Check for an invalid selection
      if((choice > 10) || (choice < 1))
        choice = 11;             // Selects invalid choice message

      switch(choice)
      {
        case 7:
          printf("777!\n");
          break;                 // Jumps to the end of the block

        case 2:
          printf("222.\n");
          break;                 // Jumps to the end of the block

        case 8:
          printf("888.\n");
          break;                 // Jumps to the end of the block

        case 11:
          printf("Try between 1 and 10.\n");
                                 // No break - so continue with the next statement

        default:
          printf("Sorry, you lose.\n");
          break;                 // Defensive break - in case of new cases
      }
      return 0;
}

The code above generates the following result.





Example 3

The following code uses switch statement to process user input.


#include <stdio.h>
/*  w w w . j a  v  a2 s. c om*/
int main(void)
{
  char answer = 0;       // Stores an input character

  printf("Enter Y or N: ");
  scanf(" %c", &answer);

  switch(answer)
  {
    case 'y': case 'Y':
      printf("You responded in the affirmative.\n");
      break;

    case 'n': case 'N':
      printf("You responded in the negative.\n");
      break;
        default:
          printf("You did not respond correctly. . .\n");
          break;
      }
      return 0;
    }

The code above generates the following result.

Example 4

uses multiple labels


#include <stdio.h>
int main(void)
{//  w  ww.j ava 2  s .  co  m
    char ch;
    int a_ct, e_ct, i_ct, o_ct, u_ct;
    
    a_ct = e_ct = i_ct = o_ct = u_ct = 0;
    
    printf("Enter some text; enter # to quit.\n");
    while ((ch = getchar()) != '#')
    {
        switch (ch)
        {
            case 'a' :
            case 'A' :  a_ct++;
                break;
            case 'e' :
            case 'E' :  e_ct++;
                break;
            case 'i' :
            case 'I' :  i_ct++;
                break;
            case 'o' :
            case 'O' :  o_ct++;
                break;
            case 'u' :
            case 'U' :  u_ct++;
                break;
            default :   break;
        }                    // end of switch
    }                        // while loop end
    printf("number of vowels:   A    E    I    O    U\n");
    printf("                 %4d %4d %4d %4d %4d\n",
           a_ct, e_ct, i_ct, o_ct, u_ct);
    
    return 0;
}

The code above generates the following result.