Logout

The Switch Statement

The switch statement is an alternative to using multiple if/else blocks. Though it generally achieves the same result, one distinct benefit it its ability to "jump in" at a certain condition and then continue on through the rest of the conditional blocks - this a multiple if/else cannot do. It is the break statement which achieves that.

The one major limit to the Java switch statement is that it makes choices based only on ints, though since chars have an int value, chars, or single character Strings can be used for the choices.

Example:

SwitchExample.java
/Users/adelaide/Public/Netbeans - All JSR Projects/String Playing Around/src/SwitchExample.java
 1 
 2 import java.io.*;
 3 
 4 public class SwitchExample {
 5 
 6     public static void main(String[] args) throws Exception {
 7         int x, y;
 8         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 9         System.out.println("Enter two numbers for operation:");
10         try {
11             x = Integer.parseInt(br.readLine());
12             y = Integer.parseInt(br.readLine());
13             System.out.println("A. Add");
14             System.out.println("S. Subtract");
15             System.out.println("M. Multiply");
16             System.out.println("D. Divide");
17             System.out.println("enter your choice:");
18             String s = br.readLine();
19             s = s.toUpperCase();
20             char c = s.charAt(0);
21             
22             switch (c) {//Note that the switch case statement can only take in integers
23                 //So in the example, we had the chars above convereted into
24                 case 65://Ascii for A
25                     System.out.println("The sum of your two numbers: " + (x + y));
26                     break;
27                 case 83://Ascii for S
28                     System.out.println("The difference of your two numbers: " + (x - y));
29                     break;
30                 case 77://Ascii for M
31                     System.out.println("The product of your two numbers: " + (x * y));
32                     break;
33                 case 68://Ascii for D
34                     System.out.println("The quotient of your two numbers: " + (x / y));
35                     break;
36                 default:
37                     System.out.println("Invalid Entry!");
38             }
39         } catch (NumberFormatException ne) {
40             System.out.println(ne.getMessage() + " is not a numeric value.");
41             System.exit(0);
42         }
43 
44     }
45 }
46 

Output:

Enter two numbers for operation:
60
20
A. Add
S. Subtract
M. Multiply
D. Divide
enter your choice:
A
The sum of your two numbers: 80


Focus on break

Above, whichever letter is input causes the only one println because of the break. Note the difference below is that the breaks are no longer there, so the switch will jump in at wherever the case is true, and then continue on down through the switch block.

SwitchExample.java
/Users/adelaide/Public/Netbeans - All JSR Projects/String Playing Around/src/SwitchExample.java
 1             .....
19             s = s.toUpperCase();
20             char c = s.charAt(0);
21             
22             switch (c) {
23                 
24                 case 65://Ascii for A
25                     System.out.println("The sum of your two numbers: " + (x + y));
26                     
27                 case 83://Ascii for S
28                     System.out.println("The difference of your two numbers: " + (x - y));
29                     
30                 case 77://Ascii for M
31                     System.out.println("The product of your two numbers: " + (x * y));
32                     
33                 case 68://Ascii for D
34                     System.out.println("The quotient of your two numbers: " + (x / y));
35                     
36                 default:
37                     System.out.println("Invalid Entry!");
38             }
46             .....

Output:

Enter two numbers for operation:
60
20
A. Add
S. Subtract
M. Multiply
D. Divide
enter your choice:
S
The difference of your two numbers: 40
The product of your two numbers: 1200
The quotient of your two numbers: 3
Invalid Entry!

So you will note above that not only is the difference line executed, but all other cases after that, actually, including right up to the "Invalid Entry!" line. So probably a "break" just before the default line would be an improvement.