Lesson #7 - Switch Statements

Updated: Dec 13, 2020

The ‘switch’ statement is used to select one of many code blocks to be executed. It is mainly used when we have the preliminary knowledge on the range of sections and only on one single variable.

The switch flow is based on the following steps:

  1. The switch expression is evaluated at the beginning of the code (and only once)

  2. The value of the expression is compared with the values of each case.

  3. 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.



6 views0 comments