Join our newsletter for the latest updates.
Java switch Statement

Java switch Statement

In this tutorial, you will learn to use the switch statement in Java to control the flow of your program’s execution with the help of examples.

The switch statement allows us to execute a block of code among many alternatives.

The syntax of the switch statement in Java is:

switch (expression) {
  case value1:
    // code to be executed if
    // expression is equal to value1
    break;
  
  case value2:
    // code to be executed if
    // expression is equal to value2
    break;
  
  ...
  ...
  
  default:
    // default statements
  }

How does the switch statement work?

The expression is evaluated once and compared with the values of each case label.

  • If there is a match, the corresponding code after the matching case label is executed.

    For example, if the value of the expression is equal to value2, the code after case value2: is executed.
  • If there is no match, the code after default: is executed.

Note: We can do the same functionality using the Java if...else...if ladder. However, the syntax of the switch statement is cleaner and much easier to read and write.


Flowchart of switch Statement

Flowchart of the Java switch statement
Flow chart of the Java switch statement

Example 1: Java switch statement

// Java Program to check the size
// using the switch...case statement

class Main {
  public static void main(String[] args) {

    int number = 44;
    String size;

    // switch statement to check size
    switch (number) {

      case 29:
        size = "Small";
        break;

      case 42:
        size = "Medium";
        break;

      // match the value of week
      case 44:
        size = "Large";
        break;

      case 48:
        size = "Extra Large";
        break;
      
      default:
        size = "Unknown";
        break;

    }
    System.out.println("Size: " + size);
  }
}

Output:

Size: Large

In the above example, we have used the switch statement to find the size. Here, we have a variable number. The variable is compared with the value of each case statement.

Since the value matches with case 44, the size variable is assigned with value Large.


break statement in Java switch...case

Notice that we have used the break statement in each case block. This is helpful to terminate the statement.

If the break statement is not used, all the cases after the matching case are executed. For example,

class Main {
  public static void main(String[] args) {

    int expression = 2;

    // switch statement to check size
    switch (expression) {
      case 1:
        System.out.println("Case 1");

        // matching case
      case 2:
        System.out.println("Case 2");

      case 3:
        System.out.println("Case 3");

      default:
        System.out.println("Default case");
    }
  }
}

Output

Case 2
Case 3      
Default case

In the above example, we haven't used the break statement. Here, the expression matches with case 2.

You can see that along with case 2, all the following cases are also executed.

This is not the desired output. Hence, it is necessary to use the break statement with each case block.


Example 2: Making Calculator using the switch statement

The program below takes three inputs from the user: one operator and 2 numbers. Based on the operator provided by the user, it performs the calculation on the numbers. Then the result is displayed on the screen.

Before you go through the program, make sure you know about Java Scanner to take input from the user.

import java.util.Scanner;

class Main {
  public static void main(String[] args) {

    char operator;
    Double number1, number2, result;

    // create an object of Scanner class
    Scanner input = new Scanner(System.in);

    // ask users to enter operator
    System.out.print("Choose an operator: +, -, *, or /: ");
    operator = input.next().charAt(0);

    // ask users to enter numbers
    System.out.print("Enter first number: ");
    number1 = input.nextDouble();

    System.out.println("Enter second number: ");
    number2 = input.nextDouble();

    switch (operator) {

      // performs addition between numbers
      case '+':
        result = number1 + number2;
        System.out.print(number1 + "+" + number2 + " = " + result);
        break;

      // performs subtraction between numbers
      case '-':
        result = number1 - number2;
        System.out.print(number1 + "-" + number2 + " = " + result);
        break;

      // performs multiplication between numbers
      case '*':
        result = number1 * number2;
        System.out.print(number1 + "*" + number2 + " = " + result);
        break;

      // performs division between numbers
      case '/':
        result = number1 / number2;
        System.out.print(number1 + "/" + number2 + " = " + result);
        break;

      default:
        System.out.println("Invalid operator!");
        break;
    }

    input.close();
  }
}

Output 1

Choose an operator: +, -, *, or /: +
Enter first number: 23
Enter second number: 
21
23.0+21.0 = 44.0

Output 2

Choose an operator: +, -, *, or /: -
Enter first number: 24
Enter second number: 
13
24.0-13.0 = 11.0

Output 3

Choose an operator: +, -, *, or /: *
Enter first number: 12
Enter second number: 
6
12.0*6.0 = 72.0

Output 4

Choose an operator: +, -, *, or /: /
Enter first number: 36
Enter second number: 
6
36.0/6.0 = 6.0

Output 5

Choose an operator: +, -, *, or /: ?
Enter first number: 12
Enter second number: 
23
Invalid operator!

In the above example, we are using the switch...case statement to perform addition, subtraction, multiplication, and division.

Note: The Java switch statement only works with: