Switch / Case / Default
Last updated
Was this helpful?
Last updated
Was this helpful?
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 statements one after another.
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:
Additionally, you can use default
case to fallback to it when all other cases fail. Default case is optional: