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. Say, What if we have ten alternatives to choose from? if we use Nested If in this situation, then programming logic will be difficult to understand.

The Else if and switch statements in Java Programming can handle these problems effectively. We discussed the 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 the code block in that Java switch 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 integer).
  • It allows us to add a default statement. If the variable value does not match any of the Java switch case expressions, then the code in the optional default statement will execute.
  • The break clause is useful to come out of it. Otherwise, all the code in the condition is executed. Whenever a break is encountered, the execution flow would directly come out of the case. Please refer Break Statement article to understand this keyword perfectly.

Java switch case Flow Chart

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

Java Switch case or statement Flow Chart

The execution flow of the Java switch statement or case 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, followed by the statement to exit.
  • If Option 1, Option 2, and Option 3 Fail, then Default STATEMENT is executed, followed by a break statement to exit from it.

Java switch case example

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

In this Java switch case example, the first three lines of code include the println, allowing the user to enter two integer values. Then we assign 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 assign the user-entered character to already declared a variable called an operator.

Next, we use the Java switch case statement with the operator as an option. If the user inputs + as an operator, 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 provides % 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 in the Java switch case will print.

Please refer to the 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 the * operator as a Java switch case expression value and enter 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 +, -, *, /, %%

switch case strings example

In this program, we are using the string data as the option.

In this Java switch case example, we divide the code using Object-Oriented Programming. First, we will create a class that holds methods to do this.

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 the 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