Signup/Sign In

Java Switch Statement

In Java, the switch statement is used for executing one statement from multiple conditions. it is similar to an if-else-if ladder.

Switch statement consists of conditional based cases and a default case.

In a switch statement, the expression can be of byte, short, char and int type.

From JDK-7, enum, String can also be used in switch cases.

Following are some of the rules while using the switch statement:

  1. There can be one or N numbers of cases.
  2. The values in the case must be unique.
  3. Each statement of the case can have a break statement. It is optional.

Syntax:

Following is the syntax to declare the switch case in Java.

	
switch(expression)
{    
case value1:    
 			//code for execution;    
 			break;  //optional  
case value2:    
 // code for execution
 break;  //optional  
......    
......
......
......
Case value n:
// code for execution
 break;  //optional  

default:     
 code for execution when none of the case is true;    
}    
	

Data Flow Diagram Of switch Block

switch block DFD

Example: Using integer value

In this example, we are using int type value to match cases. This example returns day based on the numeric value.

	
public class SwitchDemo1{ 
    public static void main(String[] args) 
    { 
int day = 3; 
        String dayName; 
        switch (day) { 
        case 1: 
dayName = "Today is Monday"; 
            break; 
        case 2: 
dayName = "Today is Tuesday"; 
            break; 
        case 3: 
dayName = "Today is Wednesday"; 
            break; 
        case 4: 
dayName = "Today is Thursday"; 
            break; 
        case 5: 
dayName = "Today is Friday"; 
            break; 
        case 6: 
dayName = "Today is Saturday"; 
            break; 
        case 7: 
dayName = "Today is Sunday"; 
            break; 
        default: 
dayName = "Invalid day"; 
            break; 
        } 
System.out.println(dayName); 
    } 
}
	

output switch Example

Example using Enum in Switch statement

As we have said, Java allows to use enum in switch cases. So we are creating an enum of vowel alphabets and using its elements in switch case.

	
public class SwitchDemo2{      
       public enumvowel{a, e, i, o, u}    
       public static void main(String args[])    
       {    
vowel[] character= vowel.values();    
           for (vowel Now : character)    
           {    
                switch (Now)    
                {    
                    case a:    
System.out.println("'a' is a Vowel");    
                        break;    
                    case e:    
System.out.println("'e' is a Vowel");    
                        break;    
                    case i:    
System.out.println("'i' is a Vowel");    
                        break;         
                    case o:    
System.out.println("'o' is a Vowel");    
                        break;    
                    case u:    
System.out.println("'u' is a Vowel");    
                        break;    
                    default:    
System.out.println("It is a consonant");    
                }    
            }    
        }    
}  
	

output-enum-switch program output


Example: String in switch case

Since Java has allowed to use string values in switch cases, so we are using string to create a string based switch case example.

	
public static void main(String[] args) {  
    String name = "Mango";  
    switch(name){  
    case "Mango":  
        System.out.println("It is a fruit");  
        break;  
    case "Tomato":  
        System.out.println("It is a vegitable");  
        break;  
    case "Coke":  
        System.out.println("It is cold drink");  
    }  
}  
}  
	

It is a fruit


Example: without break switch case

Break statement is used to break the current execution of the program. In switch case, break is used to terminate the switch case execution and transfer control to the outside of switch case.

Use of break is optional in the switch case. So lets see what happens if we don’t use the break.

	
public class Demo{      
    
    public static void main(String[] args) {  
        String name = "Mango";  
        switch(name){  
        case "Mango":  
            System.out.println("It is a fruit");  
        case "Tomato":  
            System.out.println("It is a vegitable");  
        case "Coke":  
            System.out.println("It is cold drink");  
        }  
    }
}
	

It is a fruit It is a vegitable It is cold drink

See, if we don’t use break, it executes all the cases after matching case.