A primer on the use of the Switch Decision construct, Java's second means of branching in code.
Chapters
(Please skip to relevant ones if someway familiar with concept)
======
0:00 - Introduction
0:37 - Why, How and What of the Switch
1:18 - An example of the if ... else if ... else (the alternative to the Switch)
3:00 - Scenario to be used as example ... finding if a letter is a vowel or not
5:42 - ASIDE: Commenting out code rather than deleting
6:06 - The SWITCH statement
6:54 - The main components : () and { } brackets
7:29 - The main components : case
8:12 - The main components : break keyword
9:48 - The main components : The default case
12:31 - Testing of solution
13:45 - Recap on components
16:39 - Questions / Exercises
Questions
=========
1. Based on the values of the following, state whether you think an if or a switch would be more applicable
a. The gears of a car
b. The speed of a car
c. The days of a week
d. Whether a person can vote or not
2. Using a switch statement, write a program that will allow the user to input a value and the relevant month value will then be printed. Allow for incorrect data entry, printing an applicable error message (Hint: Maybe use the default case for this).
3. Take some of the break words out of the previous solution and see the behaviour of your code. Why do you think this is happening?
www.ancodernua.ie