Monday, March 2, 2020
Using the Switch Statement for Multiple Choices in Java
Using the Switch Statement for Multiple Choices in Java 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: //lets say the user picks number 4int 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 variablecase 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, youll see that every alternative option ends with a break statement. Its very important to remember to put in the break statement. Consider the following code: //lets say the user picks number 1int 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. Its 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, Hows it going, Michael?); break; case bob: JOptionPane.showMessageDialog(null, Bob, my old friend!); break; case billy: JOptionPane.showMessageDialog(null, Afternoon Billy, hows 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.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.