Switch Statement in C

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 is as follows:-




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


Coding Tag

Coding Tag always happy to serve you in Information Technology (IT). Learn HTML, CSS, Javascript, PHP, Android, Digital Marketing, Latest IT News and many more.

Leave a Reply

avatar
  Subscribe  
Notify of