Updated: Jan 18
The ‘switch’ statement is used to select one of many code blocks to be executed. It is mainly used when we have preliminary knowledge of the range of sections and only on one variable.
The switch flow is based on the following steps:
The switch expression is evaluated at the beginning of the code (and only once)
The value of the expression is compared with the values of each case.
If there is a match, the code will be executed.
And what about the ‘break’ and ‘default’ keywords?
Break – Added per case and will stop the execution of any other code written afterward (breaks the switch block).
Default – This is an optional keyword we can add to ensure that specific code will run if there is no case match.