- switch-case statement is a multi-way branch statement.
- case statement enables the user to choose a option from a number of options.
- The switch statement requires only one argument/expression of any data type which is checked with the case constants. If the value of expression is matches with any case constant then that particular case statement is executed, If the value of expression does not match with any case constant then default block is executed.
- The break statement is used to exit from the current case block.
- Here is the syntax of switch-case statement:
case constant A:
case constant B:
- According to the above syntax if the expression matches with the case constant A then the statements present inside case constant A block will be executed, if the expression matches with the case constant B then the statements present inside case constant B block will be executed and if expression does not match with any of the case constants then the statement present inside the default block will be executed. Here is the diagram that shows the concept of switch-case statement:
- Here is an example of switch-case statement as follows:
printf(“n Enter your choice:”);
printf(“nYour choice is 1”);
printf(“nYour choice is 2”);
printf(“nYour choice is 3”);
printf(“nNo match found”);
The output of the above program is as follows:
Your choice is 3