Thursday 27 February 2020

Switch in C Language - tccicomputercoaching.com


What is Switch?

Switch is used to select any one from multiple choice. The switch statement allows us to execute one code block among many alternatives.


A switch statement tests the value of a choice variable and compares it with multiple cases. Once the case match is found, a block of statements associated with that particular case is executed. Each case in a block of a switch has a different name/number which is referred to as an identifier.

Syntax:

switch(variable)

{

case lable1:

      statement:

      break;

case lable2:

      statement:

      break;

case lable3:

       statement:

       break;

default:

     statement:

     break;

}
Example:

void main()

{

int ch;

clrscr();

       // printf ("enter ypour choice\n");

        Printf ("1 paneer makhni \n");

         printf ("2 paneeer bhurji \n");

         printf ("3 paneer mutter");

         printf ("\nenter your choice");

     scanf ("%d", &ch);

switch ( ch )

{

case 1:

      printf ("Your paneer makhni is ready\n");

      break;

case 2:

        printf ("Your paneer bhurji is ready \n");

        break;

case 3:

         printf("Your paneer mutter is ready\n");

         break;

default:

          printf("no choice");

           break;

}

     getch();

}

OP:

enter your choice:

2

Your paneer  bhurji is ready

To learn More about C Programming Langauge at TCCI .

Call @ 9825618292

Visit @ http://tccicomputercoaching.com/

No comments:

Post a Comment