Run the code to the left to see what your output should look like.
1. Write a program that allows the user to enter an int for their place in a race.
2. Using a switch statement, print gold if they placed first, silver for second, and bronze for third. If they placed anything after that... get creative with the default case.
3. Write the same exact program again, but this time use Strings. Allow the user to enter first, second, third instead of numbers.
Java Keywords & Vocabulary
New Keywords
switch - The java keyword to start a switch statement.
case - A specific test for equivalence. If that case is true, its following statements will run. This keyword has to be used inside a switch block.
default - The default case runs if none of the other cases run. This keyword has to be used inside a switch block. This is similar to the else-statement.
Vocabulary
Switch statement - A control flow structure in the java programming language.
Switch block - All of the cases and the default case inside the {}.
Possible Assessment Questions
1. If we are testing simple equivalence, if one variable equals a specific value, does it matter if we use an if statement or a switch statement?
2. If we are testing boolean expressions, does it matter if we use an if statement or a switch statement?
3. When does the default case for a switch statement run?
4. What happens if we do not put the break statement at the end of a case?
5. Write a switch statement that does (you'll see on the test :]).
6. Look at a program that implements a switch statement and predict the correct output of the program.