switch statements is used, when there is multiple alternatives but only one needs to be selected. However, the complexity of such programs increases dramatically, when the number of alternatives increases, then the programs becomes difficult to read and follow. C built a multi-way decision statement known as “switch statement” . It is like if else-if ladder statement.
rules for switch statement:
- the switch expression must be a character type or integer type
- the case value must be inter or charcter
- the case value can be used only inside switch function
- The break statement in switch case is not must. It is optional. If there is no break statement found in switch case, all the cases will be executed after matching the case value. It is known as fall through state of C switch statement.
- break is not mandatory for default statement