The if statement is the most powerful and often used decision-type command. The switch is another decision-type command. The switch statement allows a single variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.
The syntax for the switch statement is illustrated below,
switch(variable to be tested) {
case value :
// Statements
break; // optional
case value :
// Statements
break; // optional
// You can have any number of case statements.
default : // Optional
// Statements
}
The example below illustrates how to apply a switch statement to evalutate a variable of type int
System.out.println("Make your arithmetic selection from the choices below:\n");
System.out.println(" 1. Addition ");
System.out.println(" 2. Substraction ");
System.out.println(" 3. Multiplication ");
System.out.println(" 4. Division ");
System.out.println(" Your choice? ");
Scanner kbReader = new Scanner(System.in);
int choice = kbReader.nextInt();
System.out.println("\nEnter first operand");
double op1 = kbReader.nextDouble();
System.out.println("\nEnter second operand");
double op2 = kbReader.nextDouble();
System.out.println("");
switch(choice) {
case 1:
System.out.println(op1 + " plus " + op2 + "=" + (op1+op2));
break;
case 2:
System.out.println(op1 + " minus " + op2 + "=" + (op1-op2));
break;
case 3:
System.out.println(op1 + " times " + op2 + "=" + (op1*op2));
break;
case 4:
System.out.println(op1 + " divided by " + op2 + "=" + (op1/op2));
break;
default:
System.out.println("Hey dummy, enter only 1, 2, 3, or 4!");
}
In the above example, the default option is not required. The default option should be used if there is a possibility that the selected value is not one of the choices.
It is also possible to compare Strings when using switch statements. This is demonstrated below,
System.out.println("Make your arithmetic selection from the choices below:\n");
System.out.println(" A. Addition ");
System.out.println(" S. Substraction ");
System.out.println(" M. Multiplication ");
System.out.println(" D. Division ");
System.out.println(" Your choice? ");
Scanner kbReader = new Scanner(System.in);
String choice = kbReader.nextLine();
System.out.println("\nEnter first operand");
double op1 = kbReader.nextDouble();
System.out.println("\nEnter second operand");
double op2 = kbReader.nextDouble();
System.out.println("");
switch(choice) {
case "A":
System.out.println(op1 + " plus " + op2 + "=" + (op1+op2));
break;
case "S":
System.out.println(op1 + " minus " + op2 + "=" + (op1-op2));
break;
case "M":
System.out.println(op1 + " times " + op2 + "=" + (op1*op2));
break;
case "D":
System.out.println(op1 + " divided by " + op2 + "=" + (op1/op2));
break;
default:
System.out.println("Hey dummy, enter only A, S, M, or D!");
}
The break command is used to jump out of the switch statement if a conditionis not met. If the break command is omitted, each statement will continue to be evaluated as long as the first one is true. In the following example, if j is equal to 1, 2, or 3, message will be assigned to "low". If j is equal to 4, 5, or 6, message will be assigned to "high". If j is equal to 7, message will be assigned to "lucky".
String message;
int j = 2;
switch(j) {
case 1:
case 2:
case 3:
message = "low";
break;
case 4:
case 5:
case 6:
message = "high";
break;
case 7:
message = "lucky";
}
System.out.println(message);//low is printed