Using the Switch Statement for Multiple Choices in Java

Aerial view of a person working on a laptop next to scattered programming books.

Christina Morillo/Pexels

If your Java program needs to make a choice between two or three actions, an if, then, else statement will suffice. However, the if, then, else statement begins to feel cumbersome when there are a number of choices a program might need to make. There are only so many else...if statements you want to add before the code begins to look untidy. When a decision across multiple options is required, use the switch statement.

The Switch Statement

A switch statement allows a program the ability to compare the value of an expression to a list of alternative values. For example, imagine you had a drop-down menu that contained the numbers 1 to 4. Depending on which number is chosen, you want your program to do something different:

//let's say the user picks number 4
int menuChoice = 4;
switch (menuChoice)
{
case 1:
JOptionPane.showMessageDialog(null, "You chose number 1.");
break;
case 2:
JOptionPane.showMessageDialog(null, "You chose number 2.");
break;
case 3:
JOptionPane.showMessageDialog(null, "You chose number 3.");
break;
//This option gets chosen because the value 4 matches the value of
//the menuChoise variable
case 4: JOptionPane.showMessageDialog(null, "You chose number 4."); break;
default:
JOptionPane.showMessageDialog(null, "Something went wrong!");
break;
}

If you look at the syntax of the switch statement you should notice a few things:

1. The variable containing the value that needs to be compared to is placed at the top, inside the brackets.

2. Each alternative option starts with a case label. The value to be compared against the top variable comes next, followed by a colon. For example, case 1: is the case label followed by the value 1 — it could just as easily be case 123: or case -9:. You can have as many alternative options as you need.

3. If you look at the above syntax, the fourth alternative option is highlighted — the case label, the code it executes (i.e., the JOptionPane) and a break statement. The break statement signals the end of the code that needs to be executed. If you look, you'll see that every alternative option ends with a break statement. It's very important to remember to put in the break statement. Consider the following code:

//let's say the user picks number 1
int menuChoice = 1;
switch (menuChoice)
case 1:
JOptionPane.showMessageDialog(null, "You chose number 1.");
case 2:
JOptionPane.showMessageDialog(null, "You chose number 2.");
break;
case 3:
JOptionPane.showMessageDialog(null, "You chose number 3.");
break;
case 4:
JOptionPane.showMessageDialog(null, "You chose number 4.");
break;
default:
JOptionPane.showMessageDialog(null, "Something went wrong!");
break;
}

What you expect to happen is to see a dialog box saying "You chose number 1," but because there is no break statement matching the first case label, the code in the second case label also gets executed. This means the next dialog box saying "You chose number 2" will also appear.

4. There is a default label at the bottom of the switch statement. This is like a safety net in case none of the values of the case labels match the value being compared with it. It's very useful to provide a way of executing code when none of the desired options are chosen.

If you always expect one of the other options to be chosen, then you can leave out the default label, but to put one at the end of every switch statement you create is a good habit to get into. It might seem unlikely that it will ever be used but mistakes can creep into the code and it can help to catch an error.

Since JDK 7

One of the changes to the Java syntax with the release of JDK 7 is the ability to use Strings in switch statements. Being able to compare String values in a switch statement can be very handy:

String name = "Bob";
switch (name.toLowerCase())
{
case "joe":
JOptionPane.showMessageDialog(null, "Good morning, Joe!");
break;
case "michael":
JOptionPane.showMessageDialog(null, "How's it going, Michael?");
break;
case "bob":
JOptionPane.showMessageDialog(null, "Bob, my old friend!");
break;
case "billy":
JOptionPane.showMessageDialog(null, "Afternoon Billy, how's the kids?");
break;
default:
JOptionPane.showMessageDialog(null, "Pleased to meet you, John Doe.");
break;
}

When comparing two String values, it can be a lot easier if you make sure they are all in the same case. Using the .toLowerCase method means all the case label values can be in lowercase.

Things to Remember About the Switch Statement

• The type of the variable to be compared against must be a char, byte, short, int, Character, Byte, Short, Integer, String, or enum type.

• The value next to the case label cannot be a variable. It has to be a constant expression (e.g., an int literal, a char literal).

• The values of the constant expressions across all the case labels must be different. The following would result in a compile-time error:

switch (menuChoice)
{
case 323:
JOptionPane.showMessageDialog(null, "You chose option 1.");
break;
case 323:
JOptionPane.showMessageDialog(null, "You chose option 2.");
break;

• There can only be one default label in a switch statement.

• When using an object for the switch statement (e.g., String, Integer, Character) make sure it is not null. A null object will result in a runtime error when the switch statement is executed.

Format
mla apa chicago
Your Citation
Leahy, Paul. "Using the Switch Statement for Multiple Choices in Java." ThoughtCo, Aug. 25, 2020, thoughtco.com/using-the-switch-statement-for-multiple-choices-2033886. Leahy, Paul. (2020, August 25). Using the Switch Statement for Multiple Choices in Java. Retrieved from https://www.thoughtco.com/using-the-switch-statement-for-multiple-choices-2033886 Leahy, Paul. "Using the Switch Statement for Multiple Choices in Java." ThoughtCo. https://www.thoughtco.com/using-the-switch-statement-for-multiple-choices-2033886 (accessed April 19, 2024).