Line: | Conditional Line | Type: | Walkthrough |
You should have completed: | Ticket Sales | This topic leads to: | Try Parsing |
If statements are one method of selecting which statements to execute, but another exists which is more appropriate where you have an input or variable that can be one of a fixed set of values, and want to do something different for each value. In this situation a switch statement is the syntax to use (also sometimes called a select or case statement).
Note that we do the minimum amount of unique work in the different cases within the switch statement, rather than typing out the full Console.WriteLine each time.
Each case within the switch will execute if the variable in the switch statement matches that value exactly. The break keyword tells the computer to move to the end of the current block, in this case the end of the switch statement.