Java switch case

Java switch Statement: The If Else condition 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 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 switch case here.

The working functionality of the switch case in Java programming is almost the same as the If condition. As we said before, it may have n number of cases. So, it compares the expression value with the values assigned in the case block.

If both the values expression value and case value match, then code block present in that case statement will execute. Let us see the syntax of it 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 when the expression result match Option 1
    break;
 Case Option 2:
    //Execute when the expression result match Option 2
    break;
    ......
 Case Option N:
    //Execute these when the result of expression match Option N
    break;
 Default:
    /*Execute these when the result of expression 
    Not matching with any Option */
    brea;
}

The detailed explanation behind this Java switch case statements 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).
  • It allows us to add a default statement. If the variable value is not matching with any of the case expression, then the code present in the optional default will execute.
  • The break clause is useful to come out from tit. Otherwise, all the code in the condition executed. Whenever a break encountered, the execution flow would directly come out of the case. Please refer Break Statement article to understand this keyword perfectly.

Flow Chart for Java Switch Case

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

Switch Statement Flow Chart

The execution flow of the switch statement in Java is:

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

Java switch case example

This Java program allows the user to enter two integer values. It also allows selecting any Arithmetic Operators to perform arithmetic operations using switch case.

In this Java switch statement example, the first three lines of code, includes the println 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.

The next println 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.

Next, we are using the Java switch case with the operator as an option. If the user input + as an operator, then the code inside the + block will be printed.

If the user enters – as an operator, the below statement will print. When the user gives *, the following code will print.

If the user enters / as an operator, the code inside the / section will print. And if the user provide % as an operator, the println inside the % section will print.

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.

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

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 +, -, *, /, %%

Java switch case Strings example

In this program, we are using the string data as the option. In this 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 Example {
	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();
		
		MonthClass swt = new MonthClass();
		Message = swt.MonthNum(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 MonthClass {
	public String MonthNum(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 given September and Month Number  = 9";
		    break;
		case "october":
			Message = "You have given October and Month Number  = 10";
		    break;
		case "november":
			Message = "You have given November and Month Number  = 11";
		    break;
		case "december":
			Message = "You have given December and Month Number  = 12";
		    break;
		default:
			Message = "Please Provide the Proper Month name";
			break; 
		}
		return Message;
	}
}

OUTPUT 1: Let us provide march as month name.

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

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

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