Working of switch Statement in C++
The working of the switch statement in C is as follows:
- Step 1: The switch expression is evaluated.
- Step 2: The evaluated value is then matched against the present case values.
- Step 3A: If the matching case value is found, that case block is executed.
- Step 3B: If the matching code is not found, then the default case block is executed if present.
- Step 4A: If the break keyword is present in the block, then program control comes out of the switch statement.
- Step 4B: If the break keyword is not present, then all the cases after the matching case are executed.
- Step 5: Statements after the switch statement is executed.
Switch Statement in C++
The C++ Switch case statement evaluates a given expression and based on the evaluated value(matching a certain condition), it executes the statements associated with it. It is an alternative to the long if-else-if ladder which provides an easy way to dispatch execution to different parts of code based on the value of the expression.