How do I use switch statement?
Category: Introduction, viewed: 3K time(s).
The example below demonstrate a simple use of the switch statement. A switch statement can be a good replacement for a long if-else-if-else selection statement.
A switch statement handles multiple selection by passing control to one of its case statement within its body. Selection will be handled by the case statement that matches the value of the switch. switch statements can have many case but each case should have a unique value.
Each of case block should be closed by the break statement. This include the last statement whether it is a case or a default block.
If no case condition matched the switch value the control will be transferred to default block. If the default is not available the control will be transferred out side the switch.
Here is an example result of the program:
Please enter week day: 4 Day name: Wednesday