The Switch Statement allows you to select from multiple choices based on a set of fixed values for a given expression. Here are the common Switch Statement syntax:
case value1: /* execute unit of code 1 */
case value2: /* execute unit of code 2 */
default: /* execute default action */
In the Switch Statement, the selection is determined by the value of an expression that you specify, which is enclosed between the Parentheses after the keyword Switch.
The Data Type of value which is returned by expression must be an integer value otherwise the statement will not compile. The case constant expression must be an integer value and all values in case statement are equal.
When a Break Statement is executed, it causes execution to continue with the statement following the closing brace for the switch. The Break Statement is not mandatory, but if you don't put a Break Statement at the end of the statements for a case, the statements for the next case in sequence will be executed as well, through to whenever another break is found or the end of the switch block is reached. This can lead some unexpected program logics happen.
The Default Statement is the default choice of the switch statement if all cases statement are not satisfy with the expression. The Break after the default statements is not necessary unless you put another case statement below it.
printf(“Enter number: ”);
/* none when 0*/
case 0 :
/* Single when 1 */
case 1 :
/* Double when 2 */
case 2 :
/* For case 3, 4, 5 then statement will be executed till break is reached i.e. till the output of several.
case 3 :
case 4 :
case 5 :
Urgenthomework helped me with finance homework problems and taught math portion of my course as well. Initially, I used a tutor that taught me math course I felt that as if I was not getting the help I needed. With the help of Urgenthomework, I got precisely where I was weak: