Switch Statement in Java
- Difficulty Level : Easy
- Last Updated : 25 Jun, 2022
The switch statement is a multi-way branch statement. In simple words, the Java switch statement executes one statement from multiple conditions. It is like an if-else-if ladder statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression. Basically, the expression can be a byte, short, char, and int primitive data types. It basically tests the equality of variables against multiple values.
Note: Java switch expression must be of byte, short, int, long(with its Wrapper type), enums and string. Beginning with JDK7, it also works with enumerated types (Enums in java), the String class, and Wrapper classes.
![]()
Some Important Rules for Switch Statements
- There can be any number of cases just imposing condition check but remember duplicate case/s values are not allowed.
- The value for a case must be of the same data type as the variable in the switch.
- The value for a case must be constant or literal. Variables are not allowed.
- The break statement is used inside the switch to terminate a statement sequence.
- The break statement is optional. If omitted, execution will continue on into the next case.
- The default statement is optional and can appear anywhere inside the switch block. In case, if it is not at the end, then a break statement must be kept after the default statement to omit the execution of the next case statement.
Flow Diagram of Switch-Case Statement
Syntax: Switch-case
// switch statement switch(expression) { // case statements // values must be of same type of expression case value1 : // Statements break; // break is optional case value2 : // Statements break; // break is optional // We can have any number of case statements // below is default statement, used when none of the cases is true. // No break is needed in the default case. default : // Statements }Note: Java switch statement is a fall through statement that means it executes all statements if break keyword is not used, so it is highly essential to use break keyword inside each case.
Example: Consider the following java program, it declares an int named day whose value represents a day(1-7). The code displays the name of the day, based on the value of the day, using the switch statement.
Java
// Java program to Demonstrate Switch Case// with Primitive(int) Data Type// ClasspublicclassGFG {// Main driver methodpublicstaticvoidmain(String[] args){intday =5;String dayString;// Switch statement with int data typeswitch(day) {// Casecase1:dayString ="Monday";break;// Casecase2:dayString ="Tuesday";break;// Casecase3:dayString ="Wednesday";break;// Casecase4:dayString ="Thursday";break;// Casecase5:dayString ="Friday";break;// Casecase6:dayString ="Saturday";break;// Casecase7:dayString ="Sunday";break;// Default casedefault:dayString ="Invalid day";}System.out.println(dayString);}}OutputFridayOmitting the break Statement
A break statement is optional. If we omit the break, execution will continue on into the next case. It is sometimes desirable to have multiple cases without break statements between them. For instance, let us consider the updated version of the above program, it also displays whether a day is a weekday or a weekend day.
Example:
Java
// Java Program to Demonstrate Switch Case// with Multiple Cases Without Break Statements// ClasspublicclassGFG {// main driver methodpublicstaticvoidmain(String[] args){intday =2;String dayType;String dayString;// Switch caseswitch(day) {// Casecase1:dayString ="Monday";break;// Casecase2:dayString ="Tuesday";break;// Casecase3:dayString ="Wednesday";break;case4:dayString ="Thursday";break;case5:dayString ="Friday";break;case6:dayString ="Saturday";break;case7:dayString ="Sunday";break;default:dayString ="Invalid day";}switch(day) {// Multiple cases without break statementscase1:case2:case3:case4:case5:dayType ="Weekday";break;case6:case7:dayType ="Weekend";break;default:dayType ="Invalid daytype";}System.out.println(dayString +" is a "+ dayType);}}OutputTuesday is a WeekdayNested Switch Case statements
We can use a switch as part of the statement sequence of an outer switch. This is called a nested switch. Since a switch statement defines its own block, no conflicts arise between the case constants in the inner switch and those in the outer switch.
Example:
Java
// Java Program to Demonstrate// Nested Switch Case Statement// ClasspublicclassGFG {// Main driver methodpublicstaticvoidmain(String[] args){// Custom input stringString Branch ="CSE";intyear =2;// Switch caseswitch(year) {// Casecase1:System.out.println("elective courses : Advance english, Algebra");// Break statement to hault execution here// itself if case is matchedbreak;// Casecase2:// Switch inside a switch// Nested Switchswitch(Branch) {// Nested casecase"CSE":case"CCE":System.out.println("elective courses : Machine Learning, Big Data");break;// Casecase"ECE":System.out.println("elective courses : Antenna Engineering");break;// default case// It will execute if above cases does not// executedefault:// Print statementSystem.out.println("Elective courses : Optimization");}}}}Outputelective courses : Machine Learning, Big DataExample:
Java
// Java Program to Illustrate Use of Enum// in Switch Statement// ClasspublicclassGFG {// EnumpublicenumDay { Sun, Mon, Tue, Wed, Thu, Fri, Sat }// Main driver methodpublicstaticvoidmain(String args[]){// EnumDay[] DayNow = Day.values();// Iterating using for each loopfor(Day Now : DayNow) {// Switch caseswitch(Now) {// Case 1caseSun:System.out.println("Sunday");// break statement that hault further// execution once case is satisfiedbreak;// Case 2caseMon:System.out.println("Monday");break;// Case 3caseTue:System.out.println("Tuesday");break;// Case 4caseWed:System.out.println("Wednesday");break;// Case 5caseThu:System.out.println("Thursday");break;// Case 6caseFri:System.out.println("Friday");break;// Case 7caseSat:System.out.println("Saturday");break;}}}}OutputSunday Monday Tuesday Wednesday Thursday Friday SaturdayThis article is contributed by Gaurav Miglani. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
My Personal Notes arrow_drop_upRecommended ArticlesPage :Article Contributed By :Article Tags :Practice Tags :




