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.
Exercises
Section titled “Exercises”switch1 compile
Section titled “switch1 compile”Switch on a variable instead of bare values.
Show hint
Switch flow is missing a variable to evaluate the statement.switch2 compile
Section titled “switch2 compile”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.switch3 test
Section titled “switch3 test”Return a weekday name for a number using a switch.
Show hint
Ensure that each case statement in a switch block has a condition.