Switch Case in Java With Example
What is a Switch Case In Java?
Last updated on Jun 11,2021 16.1K Views
Java programming language has conditional and control statements which optimizes the logic while writing a program. Hustle free logic building using the switch case results in improved efficiency. Using a switch case in java optimizes the readability of the code while working on multiple test expressions. In this article, you will learn about switch case in java with various examples. Following are the topics discussed in this article:
- What is a Switch Case In Java?
- Rules To Remember
- Flow Chart
- Examples
- Break Statement In Switch Case
- Nested Switch Case
- Fall-Through Switch Case
- Enum In Switch Case
- String In Switch Case
What Is A Switch Case In Java?
Java switch statement is like a conditional statement which tests multiple values and gives one output. These multiple values that are tested are called cases. It is like a multi-branch statement. After the release of java 7 we can even use strings in the cases. Following is the syntax of using a switch case in Java.
switch(expression) { case value: //statement break; case value n : //statement break; default: //statement }
Rules To Remember
There are a certain rules one must keep in mind while declaring a switch case in java. Following are a certain points to remember while writing a switch case in java.
-
We cannot declare duplicate values in a switch case.
-
The values in the case and the data type of the variable in a switch case must be same.
-
Variables are not allowed in a case, it must be a constant or a literal.
-
The break statement fulfills the purpose of terminating the sequence during execution.
-
It is not necessary to include the break statement, the execution will move to the next statement if the break statement is missing.
-
The default statement is optional as well, it can appear anywhere in the block.
Flow Chart
Examples
Break Statement In Switch Case
Break statement is used to control the flow of the execution, as soon as the expression is satisfied the execution moves out the switch case block.
public class Example{ public static void main(String args[]){ int month= 7; switch(month){ case 1 : System.out.println("january"); break; case 2: System.out.println("february"); break; case 3: System.out.println("march"); break; case 4: System.out.println("april"); break; case 5: System.out.println("may"); break; case 6: System.out.println("june"); break; case 7: System.out.println("july"); break; case 8: System.out.println("august"); break; case 9: System.out.println("september"); break; case 10: System.out.println("October"); break; case 11: System.out.println("november"); break; case 12: System.out.println("december"); break; default: System.out.println("not valid"); } } }
Output: july
Nested Switch Case
Nested switch case incorporates another switch case in an existing switch case. Following is an example showing a nested switch case.
public class Example{ public static void main(String args[]){ int tech = 2; int course = 2; switch(tech){ case 1: System.out.println("python"); break; case 2: switch(course){ case 1: System.out.println("J2EE"); break; case 2: System.out.println("advance java"); } } } }
Output: advance java
Fall Through Switch Case
Whenever there is no break statement involved in a switch case block. All the statements are executed even if the test expression is satisfied. Following is an example of a fall through switch case.
public class Example{ public static void main( String args[]) { int courses = 2; switch(courses){ case 1: System.out.println("java"); case 2: System.out.println("python"); case 3: System.out.println("Devops"); case 4: System.out.println("Automation testing"); case 5: System.out.println("Hadoop"); case 6: System.out.println("AWS"); default: System.out.println("check out edureka.co for more"); } } }
Output:java python Devops Automation testing Hadoop AWS check out edureka.co for more
Enum In Switch Case
Switch case allows enum as well. Enum is basically a list of named constants. Following is an example of the use of enum in a switch case.
public class Example{ public enum day { s , m , t , w , th, fr, sa }; public static void main(String args[]){ course[] c = day.values(); for(day today : c) { switch (today){ case s : System.out.println("Sunday"); break; case m: System.out.println("Monday"); break; case t: System.out.println("Tuesday"); break; case w: System.out.println("Wednesday"); break; case th: System.out.println("Thursday"); break; case fr: System.out.println("Friday"); break; case sa: System.out.println("Saturday"); break; } } } }
Output:Sunday Monday Tuesday Wednesday Thursday Friday Saturday
String In Switch Case
After the release of Java 7, a switch case can have strings as a case. Following is an example of using string as cases in a switch statement.
public class Example{ public static void main(String args[]){ String player = "batsmen"; switch(player){ case "batsmen": System.out.println(" Batsmen are players who plays with a bat"); break; case "bowler": System.out.println("who throws the ball"); break; case "wicket-keeper": System.out.println("who keeps the ball behind the wickets"); break; case "fielder": System.out.println("who fields in the ground"); break; default: System.out.println("no entry present"); } } }
Output: Batsmen are players who play with a bat
In this article, we have discussed how we can use switch case in java with various examples. With the use of conditional statements it becomes easier to test multiple conditions at once and also generate an optimized solution of rather difficult problem. Java programming language is abundant in such concepts which makes a developer's life easier and hustle free. Kick-start your learning and master all the skills required to become a java developer. Enroll to Edureka's Java Certification program and unleash your potential into making top notch applications.
Got a question for us? please mention this in the comments section of this 'Switch Case In Java' article and we will get back to you as soon as possible.
Upcoming Batches For Java Certification Training Course
Course Name | Date | |
---|---|---|
Java Certification Training Course | Class Starts on 9th July,2022 9th July SAT&SUN (Weekend Batch) | View Details |
Java Certification Training Course | Class Starts on 13th August,2022 13th August SAT&SUN (Weekend Batch) | View Details |
Source: https://www.edureka.co/blog/switch-case-in-java/
0 Response to "Switch Case in Java With Example"
Enviar um comentário