Switch / Case / Default
A switch
statement checks the source and compares it against several declared patterns. It then executes the first case
pattern that resolves to true
. or a default
one if nothing resolves to true (and default
is provided).
A switch
statement is equivalent to chaining multiple if
statements one after another.
Syntax
Basic syntax of switch
tag is as follows:
You can chain as many cases one after another:
Additionally, compared to strongly typed languages, you can check against multiple data types at the same time:
Default fallback
Additionally, you can use default
case to fallback to it when all other cases fail. Default case is optional:
Last updated
Was this helpful?