Conditional Statements: Switch
In programming, the switch
statement is used to evaluate a given expression and execute a block of code based on the value of that expression.
It is often used as an alternative to a series of if-else
statements when there are multiple conditions to be checked.
Example:
In the above example, the switch
statement evaluates the value of myNumber
and executes the code block for the matching case. If myNumber
is 1, the code block for case 1
is executed. If myNumber
is 2, the code block for case 2
is executed. If myNumber
is 3, the code block for case 3
is executed. If myNumber
is not 1, 2, or 3, the code block for default
is executed.
The break
keyword is used to prevent the execution of the code block for the next case. If myNumber
is 2, the code block for case 2
is executed and the break
keyword prevents the code block for case 3
from being executed.
Code Challenge
To complete this challenge, fill in the blanks with the correct switch statement using the case
and default
keywords.