dsa

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

The Java switch statement executes one statement from multiple conditions. It provides an easy way to dispatch execution to different parts of code based on the value of the expression. The switch statement works with byte, short, int, long, enum types, String and some wrapper types like Byte, Short, Int, and Long. Since Java 7, you can use strings in the switch statement.

It provides an easy way to dispatch execution to different parts of code based on the value of the expression. In other words, the switch statement tests the equality of a variable against multiple values.

Syntax:


switch(expression){    
case value1:    
 //code to be executed;    
 break;  //optional  
case value2:    
 //code to be executed;    
 break;  //optional  
......    
    
default:     
 code to be executed if all cases are not matched;    
}    

For example:


       // Java program to illustrate switch-case 
class SwitchCaseDemo 
{ 
    public static void main(String args[]) 
    { 
        int i = 9; 
        switch (i) 
        { 
        case 0: 
            System.out.println("i is zero."); 
            break; 
        case 1: 
            System.out.println("i is one."); 
            break; 
        case 2: 
            System.out.println("i is two."); 
            break; 
        default: 
            System.out.println("i is greater than 2."); 
        } 
    } 
} 

Output:
i is greater than 2.

Flow Chart

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

Making Calculator using the switch statement

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 scanner = new Scanner(System.in);
        System.out.print("Enter operator (either +, -, * or /): ");

        // ask user to enter operator
        operator = scanner.next().charAt(0);
        System.out.print("Enter number1 and number2 respectively: ");

        // ask user to enter numbers
        number1 = scanner.nextDouble();
        number2 = scanner.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;
        }
    }
}
  Output:
      Enter operator (either +, -, * or /): *
Enter number1 and number2 respectively: 1
5
1+5 = 6