Switch Statement in C
×

Switch Statement in C

0 2313

The switch statement is used when we have many options to choose from and for each option, we have to perform a different task.

The syntax for the switch statement:

switch(expression) {
     case constant-expression :
     statement (s);
     break;

     case constant-expression :
     statement (s);
     break;

     case constant-expression :
     statement (s);
     break;

     /* you can have any number of case statements */
     default : /* optional */
     statement (s);
 }

Example:

#include <stdio.h>
int main()
{
    int x = 4;
    switch (x)
   {
      case 1: printf("Your Rank is 1");
      break;

      case 2: printf("Your Rank is 2");
      break;

      case 3: printf("Your Rank is 3");
      break;

      case 4: printf("Your Rank is 4");
      break;

      case 5: printf("You Rank is 5");
      break;

      default: printf("Your Ranking is Zero");
      break; 
   }
  return 0;
  }
Result: Your Rank is 4



Share:


Discount Coupons



Leave a Reply


Comments
    Waiting for your comments