English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, you will learn how to use the switch statement in Java to control the flow of program execution
In Java, we useif..else..if ladderExecute a code block between multiple blocks. However, the syntax of the if ... else ... if ladder is too long.
Therefore, we can use the switch statement instead of long if ... else ... ifLadder. The use of the switch statement makes our code more readable.
The syntax of the switch statement is:
switch (variable/expression) { case value1: // case1 statement break; case value2: // case2 statement break; .. .. .. .. .. .. default: // default statement }
The switch statement evaluates an expression or variable (expression or variable) and compares it with the value of each case label (which can be an expression).
Now, if the value matches a case label, then all statements of the matching case label will be executed.
For example, if variable/expression equals value2. In this case, all statements that match the case will be executed (case2statement).
Note that a break statement is used in each case. The break statement is used to terminate the execution of the switch statement.
This is important because if the break statement is not used, all statements after case will be executed in order until the end of the switch statement.
class Main { public static void main(String[] args) { int week = 4; String day; // switch statement to determine the day of the week switch (week) { case 1: day = "Sunday"; break; case 2: day = "Monday"; break; case 3: day = "Tuesday"; break; //matches the value of the week 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("This day is ", + day); } }
Output:
This day is Wednesday
In the above example, we use a switch statement to find out which day of the week. Here, we have a variable week that stores an integer value. Compare this value with each value inside the switch block case.
Here the value of week is4. Therefore, it matches with case 4matches. So case 4The statements inside are executed.
The following program accepts three inputs from the user:an operatorand2numbers. Based on the operator provided by the user, calculate these numbers. Then the result is displayed on the screen.
Before browsing this program, it is best if you are already familiar withJava scannerto accept user input.
import java.util.Scanner; class Main { public static void main(String[] args) { char operator; Double number1, number2, result; //Create an object of the Scanner class Scanner scanner = new Scanner(System.in); System.out.print("Enter the operator (options:") +, -, * or /); //Please enter the operator operator = scanner.next().charAt(0); System.out.print("Enter numbers separately1and numbers2: "); //Ask the user to enter numbers number1 = scanner.nextDouble(); number2 = scanner.nextDouble(); switch (operator) { //Add two numbers case "+: result = number1 + number2; System.out.print(number1 + "+" + number2 + " = " + result); break; //Subtract two numbers case "-: result = number1 - number2; System.out.print(number1 + "-" + number2 + " = " + result); break; //Multiply two numbers case "*: result = number1 * number2; System.out.print(number1 + "*" + number2 + " = " + result); break; //Divide two numbers case "/: result = number1 / number2; System.out.print(number1 + "/" + number2 + " = " + result); break; default: System.out.println("Invalid operator!"); break; } } }
Output:
Enter operator (options: +, -, * or /) * Enter numbers separately1and numbers2: 1.4 -5.3 1.4*-5.3 = -7.419999999999999
In the above example, we create a calculator using the switch statement. It performs calculations based on the operator provided by the user.