Skip to content

Switch

Beginner · Fundamentals

switch is another control flow statement. It might be a more elegant solution than if when your flow depends on a variable. You can also have a switch true as well. Check out the exercises.

Switch on a variable instead of bare values.

Show hint
Switch flow is missing a variable to evaluate the statement.

Source

A conditionless switch needs a boolean case or default.

Show hint
Switch without a condition is possible, but that case statement needs a boolean evaluation.

Source

Return a weekday name for a number using a switch.

Show hint
Ensure that each case statement in a switch block has a condition.

Source