Java Switch

By | September 29, 2021
Java Switch

Java switch statement will allow you to check for a number of condition to execute the statements. It works like an if-else-if statement. The switch statement will allow you to use byte, short, int, long, enum types, String and some wrapper types like Byte, Short, Int, and Long. with the latest version of Java, you can also use strings in the switch statement.

There is no limitation of the number of cases you will use within a single switch statement. The case value must match the types of the switch expression which can be constant or literal. You cannot use a variable for the case value. All case value must be different from each other else you will get a compile-time error. You can use an optional break statement in each case statement. If you do not use break statement then the control will go to the next case statement else the control will be out of the switch expression.

Syntax

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

Example

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

Output

Vamware
20

Example without break statement

If there is no break statement encountered then the matched case statement will get executed and the case statements after that.

Vamware
public class DemoSwitch2 {  
public static void main(String[] args) {  
   int number=20;  
   //switch expression with int value  
   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("Invalid");  
   }  
}  
}

Output

20
30
Invalid

In the above example, the case with value 20 and the case statement after that will get executed.

Example using a string as the switch expression

public class SwitchString {    
public static void main(String[] args) {    
   String val="Expert";  
   int level=0;  
   switch(val){    
   case "Beginner": level=1;  
   break;    
   case "Intermediate": level=2;  
   break;    
   case "Expert": level=3;  
   break;    
   default: level=0;  
   break;  
   }    
   System.out.println("Invalid level);  
}    
}

Output

3

Nested switch example

public class NestedSwitchDemo {    
   public static void main(String args[])  
     {  
     //C - CSE, E - ECE, M - Mechanical  
       char opt= 'C';                 
       int year = 2;  
       switch( year )  
       {  
           case 1:  
               System.out.println("English, Maths, Science");  
               break;  
           case 2:  
               switch( opt)   
               {  
                   case 'C':  
                       System.out.println("Operating System");  
                       break;  
                   case 'E':  
                       System.out.println("Micro, Logic switching theory");  
                       break;  
                   case 'M':  
                       System.out.println("Manufacturing Machines");  
                       break;  
               }  
               break;  
            }  
   }  
}

Output

Operating System

Switch example with enum

public class SwitchEnumDemo {      
      public enum Day {  Sun, Mon, Tue  }    
      public static void main(String args[])    
      {    
        Day[] DayNow = Day.values();    
          for (Day Now : DayNow)    
          {    
               switch (Now)    
               {    
                   case Sun:    
                       System.out.println("Sunday");    
                       break;    
                   case Mon:    
                       System.out.println("Monday");    
                       break;    
                   case Tue:    
                       System.out.println("Tuesday");    
                       break;         
                                 }    
           }    
       }    
}

Output

Sunday
Monday
Tuesday

Wrapper in a switch statement

It allows you to use wrapper classes like byte, short, integer, and long as the switch expression.

public class WrapperInSwitchCase {       
      public static void main(String args[])  
      {         
           Integer age = 18;        
           switch (age)  
           {  
               case (16):            
                   System.out.println("Invalid");  
                   break;  
               case (18):                
                   System.out.println("eligible for vote.");  
                   break;  
               case (65):                
                   System.out.println("senior citizen.");  
                   break;  
               default:  
                   System.out.println("Please give the valid age.");  
                   break;  
           }             
       }

Output

eligible for vote.

People are also reading:

Leave a Reply

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