Switch Statement in Java With Examples

The switch statement or switch case in java is a multi-way branch statement. Based on the value of the expression given, different parts of code can be executed quickly. In this tutorial, we will learn how to use the switch statement in Java to control the flow of program execution with examples.


The switch statement allows us to execute a block of code with multiple conditions and multiple cases. From the provided condition, it selects one statement, which matches the condition.

  • The switch statement chooses one of several statements, based on the value on an integer(int, byte, short or long) or char expression.
  • The 'switch-case' statement is used for the selection decision.
  • In the switch statement, the keyword switch is followed by an expression that must yield an integer or a character value.

Switch Case


The statement to be executed are listed below the switch statement:

  • The statement consists of one or more case statements followed by a colon(:), followed by a group of statements.
  • Each value in the case statement must be a literal or character. Notice that colon(:) is used after each value and the statements are terminated by semicolons.
  • These values must be unique, that is in a switch block there cannot be duplicated cases.
  • The last statement in every case should be a break to prevent fall through.
  • The default case handles every value not otherwise handled.


Java Switch Case Syntax:

java
switch (expression) {
     case value1: //code to be executed;
       break;
     case value2: //code to be executed;
       break;
     case value3: //code to be executed;
       break;
     default: // default statements
     }

Switch Case Example Without Break
java
//Java Program to demonstrate the use of Java Switch without break
 int number = 3;

 switch (number) {
 case 1:
   System.out.println("Case 1 : " + number);
 case 2:
   System.out.println("Case 2 : " + number);
 case 3:
   System.out.println("Case 3 : " + number);
 case 4:
   System.out.println("Case 4 : " + number);
 default:
   System.out.println("Case default : " + number);
 }

Output :

Case 3 : 3 Case 4 : 3 Case default : 3


Switch Case Example With Break
java
//Java Program to demonstrate the use of Java Switch without break
 //Java Program to demonstrate the use of Java Switch with break

int number = 3;

switch (number) {
case 1:
  System.out.println("Case 1 : " + number);
  break;
case 2:
  System.out.println("Case 2 : " + number);
  break;
  // matches the value of number
case 3:
  System.out.println("Case 3 : " + number);
  break;
case 4:
  System.out.println("Case 4 : " + number);
  break;
default:
  System.out.println("Case default : " + number);
  break;
}

Output :

Case 3 : 3

From the above two examples, we can see that adding a break statement inside the switch statement is always a good practice. Otherwise, all the statements, below the matching case will also be executed, which can cause the program to give unexpected results.