Java Switch Case

The Java If Else Statement allows us to choose between TRUE or FALSE. When there are more than two options, we simply use Nested If in Java programming. Say, What if we have ten alternatives to choose?, if we use Nested If in this situation, then programming logic will be difficult to understand. The Else if statement and Switch statement in Java Programming can handle these types of problems effectively. We already discussed the Java Else If Statement in our previous post so, let us explore the Java switch case here.

The working functionality of the switch case in Java programming is almost the same as Java If Statement. As we said before, the Switch statement may have n number of cases. So, the switch case compares the expression value with the values assigned in the case statements. If both the values expression value and case value match, then statements present in that case statement will execute. Let us see the syntax of the Java switch case for better understanding.

Java Switch Case syntax

The syntax of the Switch Statement in Java Programming is as follows:

Switch (expression) {
 Case Option 1:
    //Execute these statements when the expression result match Option 1
    break;
 Case Option 2:
    //Execute these statements when the expression result match Option 2
    break;
 Case Option 3:
    //Execute these statements when the expression result match Option 3
    break;
    ......
 Case Option N:
    //Execute these statements when the result of expression match Option N
    break;
 Default:
    /*Execute these statements when the result of expression 
    Not matching with any Option */
    break;
}

The detailed explanation behind this Java switch case syntax is:

  • The expression value should be either integer or characters (We can write the expression as n/2 also, but the result should be an integer or convertible integers).
  • Java Switch statement allows us to add a default statement. If the variable value is not matching with any of the case statements, then the code present in the default will be executed.
  • The break statement is useful to come out from the switch statement. Otherwise, all the statements in the switch condition executed. Whenever a break encountered, the execution flow would directly come out of the switch case in Java. Please refer Java Break Statement article to understand the Break statement perfectly.

Flow Chart for Java Switch Case

The following picture will show you the flow chart behind this Java Switch Statement

Flow Chart for Switch Case in C Programming

The execution flow of the Switch statement in Java is:

  • If Case = Option 1, then STATEMENT 1 is executed, followed by a break statement to exit the switch case.
  • If Case = Option 2, then STATEMENT 2 is executed, followed by a break to exit the switch case.
  • When Case = Option 3, then STATEMENT 3 will execute, it is followed by the break statement to exit the switch case.
  • If Option 1, Option 2, and Option 3 Fails, then Default STATEMENT is executed, followed by a break statement to exit from the switch case.

Java Switch Case example

This Java program allows the user to enter two integer values. It also allows selecting any Java Arithmetic Operators to perform arithmetic operations using Switch Case in Java programming language.

Please refer to Java If Statement, Java If Else Statement, Java Else If Statement, and Nested If in Java articles in Java.

/* Switch Case in C Programming example */

package ConditionalStatements;

import java.util.Scanner;

public class Switch {
 private static Scanner sc;
 public static void main(String[] args) {
 int number1, number2;
 char operator;
 sc = new Scanner(System.in); 
 System.out.println(" Please Enter two values to perform Arithmetic Operations ");
 number1 = sc.nextInt();
 number2 = sc.nextInt();
 System.out.println(" Please select any ARITHMETIC OPERATOR You wish!\n");
 operator = sc.next().charAt(0);

 switch (operator) {
 case '+':
 System.out.format("Addition of two numbers is: %d", number1 + number2);
     break;
 case '-':
 System.out.format("Subtraction of two numbers is: %d", number1 - number2);
 break;
 case '*':
 System.out.format("Multiplication of two numbers is: %d", number1 * number2);
 break;
 case '/':
 System.out.format("Division of two numbers is: %d", number1 / number2);
 break;
 case '%':
 System.out.format("Module of two numbers is: %d", number1 % number2);
 break;
 default:
 System.out.println("You have entered the Wrong operator\n");
 System.out.println("Please enter the Correct operator such as +, -, *, /, %%");
 break; 
 }
 }
}

OUTPUT 1: Let us enter * operator and entering number1 = 10, number 2 =20.

Java Switch Case 1

Let us enter the wrong operator to check the default value

 Please Enter two values to perform Arithmetic Operations 
40 60
 Please select any ARITHMETIC OPERATOR You wish!
6
You have entered the Wrong operator

Please enter the Correct operator such as +, -, *, /, %%

In this Java switch case example, the following statement will allow the user to enter two integer values. Then we are assigning the user entered values to already declared a variable called number1 and number2.

System.out.println(" Please Enter two values to perform Arithmetic Operations ");
number1 = sc.nextInt();
number2 = sc.nextInt();

The following statement will allow the User to a single character (it should be any Arithmetic operator). Then we are assigning the user entered character to already declared a variable called an operator.

System.out.println(" Please select any ARITHMETIC OPERATOR You wish!\n");
operator = sc.next().charAt(0);

Next, we are using the Java Switch case with the operator as an option. If the user enters + as an operator, then the following statement will be printed.

System.out.format("Addition of two numbers is: %d", number1 + number2);

If the user enters + as an operator, the following statement printed.

System.out.format("Subtraction of two numbers is: %d", number1 - number2);

User enters – as an operator, the below statement will print.

System.out.format("Addition of two numbers is: %d", number1 + number2);

When the user enters * as an operator, the following statement will print.

System.out.format("Multiplication of two numbers is: %d", number1 * number2);

If the user enters / as an operator, the following statement will print.

System.out.format("Addition of two numbers is: %d", number1 + number2);

If the user enters % as an operator, the following statement will print.

System.out.format("Module of two numbers is: %d", number1 % number2);

And if the user entered operator (character) is not in any of the above (operator not equal to +, -, *, / or %), the following default statement will print.

System.out.println("You have entered the Wrong operator\n");
System.out.println("Please enter the Correct operator such as +, -, *, /, %%");

Java Switch Case Strings example

In this program, we are using the string data as the Switch case option. In this java switch statement example, we are dividing the code using the Object-Oriented Programming. To do this, First, we will create a class that holds methods.

package ConditionalStatements;

import java.util.Scanner;

public class StringSwitch {
	private static Scanner sc;
	public static void main(String[] args) {
		String month, Message;
		sc = new Scanner(System.in);	
		System.out.println("Please Enter any Month Name");
		month = sc.next();
		
		SwitchClass swt = new SwitchClass();
		Message = swt.stringSwitch(month);
		System.out.println(Message);

	}
}

Within the Main program of this Java switch case example, we will create an instance of the above-specified class and call the methods.

package ConditionalStatements;
public class SwitchClass {
	public String stringSwitch(String month) {
		String Message;
		switch (month.toLowerCase()) {
		case "january":
			Message = "You have entered January and Month Number  = 1";
		    break;
		case "february":
			Message = "You have entered February and Month Number  = 2";
		    break;
		case "march":
			Message = "You have entered March and Month Number  = 3";
		    break;
		case "april":
			Message = "You have entered April and Month Number  = 4";
		    break;
		case "may":
			Message = "You have entered May and Month Number  = 5";
		    break;
		case "june":
			Message = "You have entered June and Month Number  = 6";
		    break;
		case "july":
			Message = "You have entered July and Month Number  = 7";
		    break;
		case "august":
			Message = "You have entered August and Month Number  = 8";
		    break;
		case "september":
			Message = "You have entered September and Month Number  = 9";
		    break;
		case "october":
			Message = "You have entered ApOctober and Month Number  = 10";
		    break;
		case "november":
			Message = "You have entered November and Month Number  = 11";
		    break;
		case "december":
			Message = "You have entered December and Month Number  = 12";
		    break;
		default:
			Message = "Please enter the Proper Month name";
			break; 
		}
		return Message;
	}
}

OUTPUT 1: Let us enter march as month name.

Please Enter any Month Name
march
You have entered March and Month Number  = 3

Let us enter the wrong month name to check the default value.

Please Enter any Month Name
mar
Please enter the Proper Month name