The switch case statement is used when we have multiple options and we need to perform a different task for each Switch statement in C tests the value of a variable and compates it with multiple cases. Once the case match is found, a block of Statements associated with that particular case is executed.
Syntax of Switch statements:
switch(expression)
{
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
break;
} Flow Diagram of Switch case statement:
Examples of Switch case in c:
int day = 4;
switch (day) {
case 1:
printf(“Monday”);
break;
case 2:
printf(“Tuesday”);
break;
case 3:
printf(“Wednesday”);
break;
case 4:
printf(“Thursday”);
break;
case 5:
printf(“Friday”);
break;
case 6:
printf(“Saturday”);
break;
case 7:
printf(“Sunday”);
break;
}
// Outputs “Thursday” (day 4)