C++ Switch
Selecting one of many code blocks.
C++ Switch Statements
Use the switch statement to select one of many code blocks to be executed.
This is how it works:
- The
switchexpression is evaluated once. - The value of the expression is compared with the values of each
case. - If there is a match, the associated block of code is executed.
- The
breakstatement breaks out of the switch block and stops the execution. - The
defaultstatement executes if there is no match.
Switch statements are often more efficient and readable than long chains of if..else if statements.