SWITCH STATEMENT IN JAVA

Switch Statement in Java executes a single block from multiple blocks. It is just like if else if statement that we have discussed in our previous tutorial.

Table of Contents

[hide]
  • Switch Statement in Java
  • Important Points to Remember
  • Java Switch fall through

Switch Statement in Java

If there are several options or conditional blocks then it is not recommended to go for nested if else statements because it reduces the readability of the program. In this situation, we should always use Java Switch Statements.

Java Switch Syntax :

switch (expression){case a :// conditional block executionbreak;case b :// conditional block executionbreak;default :// conditional block execution}

Important Points to Remember :

  • Argument to the switch statement allows only few Data Types i.e byte, short, char, int ( with its corresponding Wrapper Classes) ,enum, String. Rest of the data types i.e float, double, boolean and long are not allowed.

  • Both the case and default in Java Switch are optional. Without case and default also empty java switch statement is a valid syntax.

  • Independent Statemets in switch are not allowed i.e( priting something), it should be under case or default.

  • case value must be the type of expression. Case value must be a constant expression, variables are not allowed.

  • case label range must be within the switch argument type range.

  • Within the switch statement if any case matches, then from that case onwards all the statements will be executed until break or end of the switch is there. This is called as Fall through in Switch

  • dublicate case labels are not allowed.

Example

copyimage
// Java program to illustrate the use of Java switch case
import java.util.*;
public class SwitchCaseExample{
    public static void main(String args[]){  
        Scanner sc= new Scanner(System.in); 
        System.out.print("Enter any Week Day ");
        int day= sc.nextInt(); 
        String dayName= ""; 
            switch (day) {
            case 1 :
                dayName = "Monday";
                break;
            case 2 :
                dayName = "Tuesday";
                break;
            case 3 :
                dayName = "Wednesday";
                break;
            case 4 :
                dayName = "Thursday";
                break;
            case 5 :
                dayName = "Friday";
                break;
            case 6 :
                dayName = "Saturday";
                break;
            case 7 :
                dayName = "Sunday";
                break;
            default :
                dayName = "Invalid day";
                break;
            }
        System.out.print("Entered Day is : " + dayName);
     }
}

OUTPUT :

Enter any Week Day : 6Entered Day is : Saturday

NOTE : Though break statement is optional in switch case, it saves a lot of time if we have multiple conditions to be checked. As soon as Java finds break keyword, it ignores the rest of the code thus saves the execution time and improves performance.

Java Switch fall through

Java Switch fall through means that it executes all the case blocks if a break statement is not present. Above we have made a note that if Java finds a break keywrd it ignores rest of the case blocks. But if it does not find any break it executes all the cases until next break of default is found.

Syntax :

switch (expression){case x :// conditional block executioncase y :// conditional block executiondefault :// conditional block execution}

Example

copyimage
// Java program to illustrate the use of Java switch fall through
import java.util.*;
public class FallThroughExample{
    public static void main(String args[]){  
        Scanner sc= new Scanner(System.in); 
        System.out.print("Enter any number ");
        int num= sc.nextInt(); 
            switch (num) {
            case 1 :
                System.out.println("Entered in first block");
            case 2 :
                System.out.println("Entered in second block");
            case 3 :
                System.out.println("Entered in third block"); 
            case 4 :
                System.out.println("Entered in fourth block"); 
                break;
            default :
                System.out.println("Entered in default block"); 
                break;
          }
     }
}

OUTPUT :

Enter any number : 2Entered in second blockEntered in third blockEntered in fourth block

EXPLANATION

As you can see in above program that we have provided the input 2 to the switch as an argument. First it will enter the switch, it will look for the case to be matched, in out scenario case 2 matches the argument.

Case 2 will get executed and it will print the statement. Now it will look for the break keyword, in our case we have not provided the break keyword hence it will go to the next block for execution.

Next block also does not have break keyword, hence it will fall back until it finds the break keyword or default. There is a break keyword in fourth block hence it will print all the values upto this.

Hope you have learnt Switch Statement in Java , now its time to dive into Java for loop tutorial.