Switch Case
The syntax of the switch case is as follows:
switch(number)
{
case 1: // code to be executed if number = 1;
break;
case 2: // code to be executed if number = 2;
break;
default: // code to be executed if number doesn't match any cases
}
Switch statements have their own set of syntactic symbols.switch allows us to execute multiple statements for the different values of a single variable called switch variable.
#include
#include
void main()
{
int number = 3;
switch (number)
{
case 1: printf("Choice is 1");
break;
case 2: printf("Choice is 2");
break;
case 3: printf("Choice is 3");
break;
default: printf("Choice other than 1, 2 and 3");
break;
}
getch();
}
Output
Choice is 3
Another example
#include
#include
void main()
{
int number;
printf("Enter the value of number:");
scanf("%d", &number);
switch (number)
{
case 1: printf("Choice is 1");
break;
case 2: printf("Choice is 2");
break;
case 3: printf("Choice is 3");
break;
default: printf("Choice other than 1, 2 and 3");
break;
}
getch();
}
Output
Enter the value of number:5
Choice other than 1, 2 and 3