Java Switch Case Statement

After If-Else statement we are moving to Java Java Switch Case. In this tutorial, we will discuss what is Java Switch Case and how it works?

What is Java Switch Case?

Java Switch Case Statement is worked similarly to IF-ELSE-IF statements in java. A switch is a multiple-choice decision-making selection statement. It is used when we want to select only one out of multiple cases.

Syntax:

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

}

This is flow chart of how java switch case statement is working.

java switch case

Some Points to be noted:

  1. Duplicate case expressions are not allowed.
  2. There can be a number of case values for a switch expression.
  3. The case value can have an optional default label.
  4. The Java switch statement must be a byte, short, int, long, and string.
  5. The break is used inside the switch to terminate the case statement sequence.
  6. Each case statement has a break statement which can be optional.

Example of Switch case statement:

public class SwitchExample {  
public static void main(String[] args) {  
   
    int numb=10;  
     
    switch(numb){  
    case 10: System.out.println("10");      //Case statements  
    break;  
    case 30: System.out.println("30");  
    break;  
    case 20: System.out.println("20");  
    break;   
    default:System.out.println("Invalid......!");  //Default case statement  
    }  
}  
} 

Output would be:

10

Java Switch Example where we are omitting the break statement

public class SwitchExample2 {  
public static void main(String[] args) {  
    int number=30;   
    switch(number){  
    //switch cases without break statements  
    case 10: System.out.println("10");  
    case 20: System.out.println("20");  
    case 30: System.out.println("30");  
    default:System.out.println("Not found in 10, 20 or 30");  
    }  
}  
}

Output would be:

20
30
Not found in 10, 20 or 30

Java Program to demonstrate the use of Enum in the switch statement:

public class JavaSwitchEnumExample {      
       public enum Day {  Sun, Mon, Tue, Wed, Thu, Fri, Sat  }    
       public static void main(String args[])    
       {    
         Day[] NowDays = Day.values();    
           for (Day Now : NowDays)    
           {    
                switch (Now)    
                {    
                    case Sun:    
                        System.out.println("Sunday");    
                        break;    
                    case Mon:    
                        System.out.println("Monday");    
                        break;    
                    case Tue:    
                        System.out.println("Tuesday");    
                        break;         
                    case Wed:    
                        System.out.println("Wednesday");    
                        break;    
                    case Thu:    
                        System.out.println("Thursday");    
                        break;    
                    case Fri:    
                        System.out.println("Friday");    
                        break;    
                    case Sat:    
                        System.out.println("Saturday");    
                        break;    
                }    
            }    
        }    
}    

Must Read:

1. Which of the following is an alternative to SWITCH in the Java language?
A) break, continue
B) for, while
C) if, else
D) goto, exit
Ans: C) if,else
2. A SWITCH statement accepts ___ type of data as input.
A) int
B) short
C) byte
D) All of the above
Ans: D) All of the above

Leave a Reply

Your email address will not be published. Required fields are marked *