Switch case are alternative of if else if ladder
There is one problem with the if statement: the program's complexity increases whenever the number of if statements increases. If we use multiple if-else statements in the program, the code might become difficult to read and comprehend. Sometimes it also even confuses the developer whom himself wrote the program. Using the switch statement is the solution of this problem.
int a = 2;
switch (a)
{
case 2:
break;
// after checking case 2 it won't go to case 3 instead it will exit and it will not print the default case.
printf("value is 2");
case 3:
printf("value is 3");
default:
printf("nothing matched");
}
#include <stdio.h>
int main()
{
int i = 9;
switch (i)
{
case 5:
printf("Value is 7");
break;
case 0:
printf("Value is 8");
break;
case 9:
printf("Value is 9");
break;
default:
printf("Value is not present");
break;
}
return 0;
}
switch (expression 1)
{
Case 1 : printf(“Switch Statement 1”);
switch (expression 2)
{
Case 1 : printf(“Switch Statement 2”);
Break;
Case 2 : Do this;
}
break;
Case 2 : Do this;
}