In this tutorial you will learn to use the switch command to control the flow of your program. In Java, the if..else..if ladder executes code from many blocks.
The switch command can replace long ladders that make the code legible.
The syntax of the switch command is as follows:
switch (variable / expression) {
case value1:
// statements
break;
case value2:
// statements
break;
.. ..…
.. ..…
default:
// statements
}
The switch command evaluates the expression (mostly variable) and compares it with the values (can be an expression) of each label.
The switch command executes all commands of the matching label.
Suppose the variable / expression is equal to value2. In this case, all commands related to this adaptive case are executed.
Note: Use the phrase break. This terminates the execution of the switch statement. Break statements are important because if they are not used, all commands after the matching tag will be executed in sequence until the end of the switch statement.
Flowchart command switch
It should also be noted that the switch command in Java only works with the following:
- Initial data types: byte, short, char and int
- Types (enum (enum Java
- String class
- Character, Byte, Short and Integer classes.
Example 1: Switch command in Java
- class Day {
- public static void main (String [] args) {
- int week = 4;
- String day;
- switch (week) {
- case 1:
- day = “Sunday”;
- break;
- case 2:
- day = “Monday”;
- break;
- case 3:
- day = “Tuesday”;
- break;
- case 4:
- day = “Wednesday”;
- break;
- case 5:
- day = “Thursday”;
- break;
- case 6:
- day = “Friday”;
- break;
- case 7:
- day = “Saturday”;
- break;
- default:
- day = “Invalid day”;
- break;
- }
- System.out.println (day);
- }
- }
Output
Wednesday
Example 2: Switch command in Java
The following program receives three inputs from the user: an operator and 2 numbers. Performs calculations based on numbers and operators entered. The result is then displayed.
We used the scanner object to get input from the user.
- import java.util.Scanner;
- class Calculator {
- public static void main (String [] args) {
- char operator;
- Double number1, number2, result;
- Scanner scanner = new Scanner (System.in);
- System.out.print (“Enter operator (either +, -, * or /):”);
- operator = scanner.next (). charAt (0);
- System.out.print (“Enter number1 and number2 respectively:”);
- number1 = scanner.nextDouble ();
- number2 = scanner.nextDouble ();
- switch (operator) {
- case ‘+’:
- result = number1 + number2;
- System.out.print (number1 + “+” + number2 + ”=” + result);
- break;
- case ‘-‘:
- result = number1 – number2;
- System.out.print (number1 + “-” + number2 + ”=” + result);
- break;
- case ‘*’:
- result = number1 * number2;
- System.out.print (number1 + “*” + number2 + ”=” + result);
- break;
- case ‘/’:
- result = number1 / number2;
- System.out.print (number1 + “/” + number2 + ”=” + result);
- break;
- default:
- System.out.println (“Invalid operator!”);
- break;
- }
- }
- }
Output
Enter operator (either +, -, * or /): *
Enter number1 and number2 respectively: 1.4
-5.3
1.4 * -5.3 = -7.419999999999999