Switch statements express conditionals across many branches.
Here’s a basic switch, implemented using a dictionary in Python:
You can use if-elif-else to separate multiple conditions in the same case statement. We use the optional else case in this example as well:
Using a switch without an expression is an alternate way to express if/else logic. Here we also show how the case expressions can be non-constants:
A type switch compares types instead of values. You can use this to discover the type of a variable. In this example, the variable t will have the type corresponding to its clause: