A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case. Show SyntaxThe syntax of enhanced for loop is − switch(expression) { case value : // Statements break; // optional case value : // Statements break; // optional // You can have any number of case statements. default : // Optional // Statements } The following rules apply to a switch statement −
Flow DiagramExamplepublic class Test { public static void main(String args[]) { // char grade = args[0].charAt(0); char grade = 'C'; switch(grade) { case 'A' : System.out.println("Excellent!"); break; case 'B' : case 'C' : System.out.println("Well done"); break; case 'D' : System.out.println("You passed"); case 'F' : System.out.println("Better try again"); break; default : System.out.println("Invalid grade"); } System.out.println("Your grade is " + grade); } } Compile and run the above program using various command line arguments. This will produce the following result − OutputWell done Your grade is C java_decision_making 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:
2. Nested Switch Case 3. Fall-Through Switch Case 4. Enum In Switch Case 5. 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) Rules To RememberThere 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.
Flow ChartExamplesBreak Statement In Switch CaseBreak 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{ Output: july Nested Switch CaseNested switch case incorporates another switch case in an existing switch case. Following is an example showing a nested switch case. public class Example{ Output: advance java Fall Through Switch CaseWhenever 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{ Enum In Switch CaseSwitch 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{ String In Switch CaseAfter 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{ 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. If you wish to check out more articles on the market’s most trending technologies like Artificial Intelligence, DevOps, Ethical Hacking, then you can refer to Edureka’s official site. Do look out for other articles in this series that will explain the various other aspects of Java.
|