Skip to main content

Switch Components

The switch statement has several components:

  • The initial switch statement. This statement includes the value to test (in this case, button) in parentheses.
  • Braces ({ and }) enclose the contents of the switch statement, similar to a function or an if statement.
  • One or more case statements, each of which specifies a value to compare with the value in the switch statement. If the values match, the statements after the case statement are executed. Otherwise, the next case is tried.
  • The break statement is used to end each case. This skips to the end of the switch. If break is not included, statements in multiple cases might be executed whether they match or not.
  • Optionally, the default case can be included and followed by one or more statements that are executed if none of the other cases were matched.

Next: User Defined Functions