About Lesson
Switch…case
it is used when we want to check assignment of different values to a single variable and execute corresponding code.
//example program switch...case
#include<stdio.h>
void main()
{
int x;
printf("Enter any number for 2,4,6 or 8 :");
scanf("%d",&x);
switch(x)
{
case 2: printf("TWO"); break;
case 4: printf("FOUR"); break;
case 6: printf("SIX"); break;
case 8: printf("EIGHT"); break;
default: printf("Invalid Input");
}
printf("\nRest of code in main");
}
sometimes it works as alternative to multiple if.
break transfers the program control to outside of the current block.