Switch statements are used to perform different actions based on different conditions.
Here we have a long example, where the user types a command, and the program runs the code for a given command. (This is sometimes referred to as a shell or command loop).
Notice how we first get a command from the user, look at each possibility, and do something specific if we find a matching command string. Otherwise, we print an error message.
String lastCommand = getCommandFromUser();
switch(lastCommand){
case "exit":
System.out.println("so long!");
break;
case "run":
System.out.println("running simulation...");
runSim();
break;
case "rename":
renameSim();
break;
case "delete":
if (makeSureDeleteIsOkay()) {
deleteSim();
} else {
System.out.println("delete cancelled...");
}
break;
case "new":
createNewSim();
break;
case "help":
showHelpToUser();
break;
default:
System.out.println("command not found: try again or type help");
}Switch statements can be quite elaborate (and in this case much better than a whole lot of IF statements).
Here’s the exercise from the if section. Oftentimes, you can write a switch statement in if statements, or a bunch of if statements as a switch statement. This time, you should use a switch statement.
Exercise: Age Verification with Switch Statement
Write code to check if a user is old enough to drink. (Using a switch)
-
if the user is under 18. Print out "cannot party with us"
-
Else if the user is 18 or over. Print out "party over here"
-
Else print out "I do not recognize your age"
Finally, make sure to change the value of the age variable to output different results.