![]() |
VOOZH | about |
Prerequisite - Switch Statement in C Switch is a control statement that allows a value to change control of execution.
Output:
Choice is 2
Following are some interesting facts about switch statement. 1) The expression used in switch must be integral type ( int, char and enum). Any other type of expression is not allowed.
Output:
Compiler Error: switch quantity not an integer
In Java, String is also allowed in switch (See this) 2) All the statements following a matching case execute until a break statement is reached.
Output:
Choice is 2 Choice is 3 Choice other than 1, 2 and 3
Output:
Choice is 2 Choice is 3 Choice is 4 After Switch
3) The default block can be placed anywhere. The position of default doesn't matter, it is still executed if no match found.
Output:
Choice other than 1 and 2
4) The integral expressions used in labels must be a constant expressions
Output:
Compiler Error: case label does not reduce to an integer constant
5) The statements written above cases are never executed After the switch statement, the control transfers to the matching case, the statements written before case are not executed.
Output:
Choice is 1
6) Two case labels cannot have same value
Output:
Compiler Error: duplicate case value