How To Use Java Switch Statement With Examples

How To Use Java Switch Statement With Examples
How To Use Java Switch Statement With Examples 


How To Use Java Switch Statement With Examples 

Switch Statement: A Java switch statement executes one case statement from multiple conditions.

Switch Statement same if-else-if ladder statement. The switch statement works with a byte, short, int, long, enum types, 

String and some wrapper types like Byte, Short, Int, and Long.
Switch Statement can use strings in the switch statement.

Some Important Points of Switch Statement in Java

1. Switch Statement can be one or n of case values for a switch expression. 

2. Switch case value must be of switch expression type only.
 
3. Switch Statement does not allow variables. A switch case value must be uniquely identified. 
In case of duplicate value, compile-time error in switch program.

4. Switch Every case statement can have a break statement that can be used in the switch program. 

5. When starting execution of program then cases are matched in switch 
expression.

6. When cases are match executed case match line then compiler break switch Statement.

7. Switch Statement case value can have a default value is optional.

Some Others Programming Language Switch Statement 

👉C++ Switch Statement 

👉Switch Statement 


Syntax of Switch Statement in java 

switch(expression)

{


case '1':


//case one execute ;


break;


case '3':


//case three execute;


break;


default:


//no case match is default case is executed;


}



How To Use switch statement in java?

public class Month

{


public static void main(String[] args) 


{    


int month=3;  


String monthString=" ";   

switch(month)


{    


     
case 1: monthString="1:January";  


break;    


case 2: monthString="2:February";  


break;    


case 3: monthString="3:March";  


break;    


case 4: monthString="4:April";  


break;    


case 5: monthString="5:May";  


break;    


case 6: monthString="6:June";  


break;    


case 7: monthString="7:July";  


break;    


case 8: monthString="8:August";  


break;    


case 9: monthString="9:September";  


break;    


case 10: monthString="10:October";  


break;    


case 11: monthString="11:November";  
    

break;    


case 12: monthString="12:December";  

break;    


default:System.out.println("Invalid Month!");    


}    
      

System.out.println(monthString);  

}    

}   

*****OUTPUT*****

3:March


Switch Statement Nested in java

A switch statement inside other switch statements in Java. It is called a nested switch Statement.


How To Use enum switch statement in java?

public class Enum


{      


public enum Days 


{  Sun, Mon, Tues, Wed, Thu, Fri, Sat  }    



public static void main(String args[])    


{    


Days[] Day = Days.values();    


for (Days d: day now)    


{    


switch (d)    


{    


case Sun:    


System.out.println("Sunday");    


break;    


case Mon:    


System.out.println("Monday");    


break;    


case Tues:    


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;    


}    


}    


}    


}    


*****OUTPUT*****


Sunday 


Monday 


Tuesday 


Wednesday 


Thursday 


Friday 


Saturday 


Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.