Switch statement
The switch statement is similar to the nested if..else statement. It’s mostly a matter of preference which you use, switch statement can be slightly more efficient and easier to read.
Syntax:
switch( expression )
{
case constant-expression1: statements1;
case constant-expression2: statements2;
case constant-expression3: statements3;
default : statements4;
}
Example program:
#include <stdio.h>
void main ()
{
char grade = 'B';
switch (grade)
{
case 'A':
printf ("Excellent\n");
break;
case 'B':
printf ("Good\n");
break;
case 'C':
printf ("Satisfied\n");
break;
case 'F':
printf ("Fail\n");
break;
default:
printf ("Check your grade\n");
}
}
Using break keyword: If a condition is met in the case of a change, the following case clause shall continue to apply also if it is not explicitly specified that the performance must exit the switch instruction. This is achieved by using the break keyword.
