Section 10.1 The switch()
Statement
The switch()
statement is a selection control structure that allows one of several alternatives to be executed, depending on the value a variable takes. To be more precise, a switch()
statement allows a variable to be compared to a list of values. Each value is called a case. If the variable equals a value from the list then the instructions following that particular case are executed.
Let’s take a look at an example:
admin.....open in new window
Subsection 10.1.1 switch()
Statement Practice
Activity 10.1.
Please complete the program below so that it prints out the correct month, depending on the value of the variable “month”. For example, if month=3
, the program should print
We are in the month of March.
Your program should use a switch()
statement.
admin.....open in new window
When you are done, please paste your code into the code submission box below: