The switch-case construct allows you to select from multiple choices based on a set of fixed values for a given expression. Here is the common switch-case syntax:
Control passes to the statement whose case constant-expression matches the value of switch ( expression ).
The switch statement can include any number of case instances, but no two case constants within the same switch statement can have the same value.
Execution of the statement body begins at the selected statement and proceeds until the end of the body or until a break statement transfers control out of the body.
Without break, the program continues to the next case, executing the statements until a break or the end of the statement is reached. In some situations, this continuation may be desirable.
The default statement is executed if no case expression is equal to the value of switch (expression). If the default statement is omitted, and no case match is found, none of the statements in the switch body are executed.
There can be at most one default statement. The default statement need not come at the end; it can appear anywhere in the body of the switch statement.
No comments