Switch Statement
Let's expand our skill set by learning another conditional statement.
We'll cover the following
What is a switch
statement?
The switch
statement is like an if
statement. It’s useful if we have several options to choose from. Anything that can be done with a switch
statement can also be done with an if
statement, so it’s best to select whichever makes the code easiest to understand.
With switch
statements, each option is a case
, and there’s also a default
option if none matches (like an else
statement). In the case
, curly brackets ({}
) are not needed. A case
ends with a break;
.
Get hands-on with 1400+ tech skills courses.