Java Switch Case

Java Switch Case statement is used to execute a block of statements, selectively, from many different blocks (cases).

Java switch case statement could be related to a if-else-if ladder statement where a single expression is matched with different values in the if conditions.

In this tutorial, we will learn the syntax of switch statement and go through some example programs to understand the usage of switch statement.

Syntax of Java Switch statement

A switch statement contains an expression, one or more case blocks and an optional default block.

Following is the syntax of a switch statement in Java.

switch(expression) {
   case value1:
     // statement(s)
     break;
   case value2:
     // statement(s)
     break;
   default:
     // statement(s)
 }

Firstly, Java evaluates the expression to a value. This value is matched against the case values written inside switch statement.

If a case value matches with the expression value, then the corresponding block of statements are executed.

If there no match with any of the case values, then the default block at the end of the switch statement is executed. Therefore you write the code in default block, that has to be executed when no case block is selected for execution.

Example 1 – Java Switch Case

Following is a simple example program containing switch case. The expression evaluates to an integer. This integer value is matched against the case values. The first case that gets matched shall be executed.

Example.java

public class Example {

	public static void main(String[] args) {
		someFunction(5, 4);
		someFunction(18, 6);
		someFunction(18, 7);
	}
	
	public static void someFunction(int a, int b) {
		System.out.println("a="+a+", b="+b);
		switch(a%b) {
			case 0:
				System.out.println("a/b leaves a reminder of 0.");
				System.out.println("a is exactly divisible by b.");
				break;
			case 1:
				System.out.println("a/b leaves a reminder of 1.");
				break;
			case 2:
				System.out.println("a/b leaves a reminder of 2.");
				break;
			default:
				System.out.println("a/b leaves a reminder >2.");
		}
		System.out.println();
	}

}

Run this program, and you shall see the following output printed to your console.

a=5, b=4
a/b leaves a reminder of 1.

a=18, b=6
a/b leaves a reminder of 0.
a is exactly divisible by b.

a=18, b=7
a/b leaves a reminder >2.

For values a=5, b=4; a/b is 1. So program executes case 1: block.

For values a=18, b=6; a/b is 0. So program executes case 0: block.

For values a=18, b=7; a/b is 4. There is no match, hence program executes default: block.

Example 2 – Java Switch Case with Strings

In this example, expression for switch statement evaluates to a String.

Example.java

public class Example {

	public static void main(String[] args) {
		someFunction("Student");
		someFunction("Teacher");
		someFunction("parent");
		someFunction("PRINCIPAL");
		someFunction("guest");
	}
	
	public static void someFunction(String role) {
		switch(role.toLowerCase()) {
			case "student":
				System.out.println("Join your friends.");
				break;
			case "teacher":
				System.out.println("Hello Teacher. Please join your class.");
				break;
			case "principal":
				System.out.println("Good day Principal!");
				System.out.println("Students, teachers and parents are waiting for you.");
				break;
			default:
				System.out.println("Welcome to our ABC School sir. We are glad to have you here.");
		}
		System.out.println();
	}

}

Run this program, and you shall see the following output printed to your console.

Join your friends.

Hello Teacher. Please join your class.

Welcome to our ABC School sir. We are glad to have you here.

Good day Principal!
Students, teachers and parents are waiting for you.

Welcome to our ABC School sir. We are glad to have you here.

Conclusion

In this Java Tutorial, we learned about Java Switch case statement and learned how to use switch statement in Java programs.