Utilizor
Contact Us

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 switch expression 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 break statement breaks out of the switch block and stops the execution.
  • The default statement executes if there is no match.

Switch statements are often more efficient and readable than long chains of if..else if statements.