Switch Case in Java


Definition

The switch statement is a statement from the Java multiway branch. It provides an easy way to execute executions based on the quality of the expression in different parts of your code. As such, the switch statement is used because it always provides a better alternative than a larger series of if-else statements. The general form of a switch statement is as follows:

switch (expression)
{
 case value1:
  //statement sequence
  break;
 case value2:
  //statement sequence
  break;
 .
 .
 .
 case valueN:
  //statement sequence
  break;
 default:
  //default statement sequence
}

Switch Case Example 1

The following example creates a switch case and sends an integer value inside it. Inside the switch case, three cases and default: have been created. Now the statement of the case that the user will press the number from 1,2,3 will work. In this example, the "case 2" statement worked for user 2 to press. The work of the break statement is shown in the following tutorials.

Program
import java.util.Scanner;
public class Main
{
	public static void main(String[] args) 
	{
	    Scanner sc = new Scanner(System.in);
	    System.out.println("Choose your options \n 1 \n 2 \n 3\n");
	    int value = sc.nextInt();
	    switch(value)
	    {
	        case 1:
	            System.out.println("You entered first number");
	            break;
	        case 2:
	            System.out.println("You entered second number");
	            break;
	        case 3:
	            System.out.println("You entered third number");
	            break;
	        default:
	            System.out.println("You entered wrong options");
	    }
	}
}
Run

Switch Case Example 2

In the following example, four cases of switch case have been made and in them statement of addition, subtraction, multiplication, division has been written. Multiplication statement has worked for user 3 pressed on the output.

Program
import java.util.Scanner;
public class Main
{
	public static void main(String[] args) 
	{
	    Scanner sc = new Scanner(System.in);
	    int fn,ln;
	    System.out.println("Choose your options \n 1 for addition \n 2 for subtraction \n 3 for multiplication \n 4 for division");
	    int value = sc.nextInt();
	    switch(value)
	    {
	        case 1:
	            System.out.println("You Choose Addition(+)");
	            System.out.println("Enter your first number");
	            fn=sc.nextInt();
	            System.out.println("Enter your second number");
	            ln=sc.nextInt();
	            int sum=fn+ln;
	            System.out.println(fn+" + "+ln+" = "+sum);
	            break;
	        case 2:
	            System.out.println("You Choose Subtraction(-)");
	            System.out.println("Enter your first number");
	            fn=sc.nextInt();
	            System.out.println("Enter your second number");
	            ln=sc.nextInt();
	            int sub=fn-ln;
	            System.out.println(fn+" - "+ln+" = "+sub);
	            break;
	        case 3:
	            System.out.println("You Choose Multiplication(*)");
	            System.out.println("Enter your first number");
	            fn=sc.nextInt();
	            System.out.println("Enter your second number");
	            ln=sc.nextInt();
	            int mul=fn*ln;
	            System.out.println(fn+" * "+ln+" = "+mul);
	            break;
	        case 4:
	            System.out.println("You Choose Division(/)");
	            System.out.println("Enter your first number");
	            fn=sc.nextInt();
	            System.out.println("Enter your second number");
	            ln=sc.nextInt();
	            int div=fn/ln;
	            System.out.println(fn+" / "+ln+" = "+div);
	            break;
	        default:
	            System.out.println("You entered wrong options");
	    }
	}
}
Run

Nested switch Statements

You can use another switch inside the outer switch case. This is called a nested switch.

Program
import java.util.Scanner;
public class Main
{
	public static void main(String[] args) 
	{
	    Scanner sc = new Scanner(System.in);
	    int anothervalue;
	    System.out.println("Choose your options \n 1 for another switch \n 2 for none \n");
	    int value = sc.nextInt();
	    switch(value)
	    {
	        case 1:
	            System.out.println("Enter your options");
	            anothervalue=sc.nextInt();
	            switch(anothervalue)
	            {
	                case 1:
	                    System.out.println("You pressed 1");
	                    break;
	                default:
	                    System.out.println("You choose wrong options");
	            }
	            break;
	        case 2:
	            System.out.println("You Choose 2");
	            break;
	        default:
	            System.out.println("You entered wrong options");
	    }
	}
}
Run