While programming, it is very difficult to handle dealing with different actions on different conditions. Though in Java and many other programming languages, statements like if-else, if-else-if are used in various conditions. But what if there are ‘n’ number of scenarios that need separate actions on each condition? Multiple if and else-if statements at this point can cause confusion to the programmer at some point in the code as it involves multiple braces and a set of statements for each condition. To handle such situations in Java, Switch-Case statements are used for the ease of programmer and reduce code line and complexity.
The switch statement is a branch statement. The case is a keyword that is used with the Switch statement. It performs the execution of statement/statements when the value of the expression is matched with the case value, and the code of the particular statements is ended by break keyword. The Case values in Java can be byte, int, short, byte data types. With the Java JDK7, the value of the case can also be String, Wrapper and enumerated types. If any of the values of the Case does not match with the expression, then the default statement is executed. Though the default and break keywords are not mandatory in Switch-Case statements.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
As described above, Case in a particular Switch statement is executed when the value of the expression matches with the Case value. If none of the value matches case values, then the default statement defined in the Switch block is executed; otherwise, nothing got executed.
Important points to summarise:
switch (expression) { // case statements // same data type for switch expression and case value Case value1: //Statement /statements to be executed break; case value 2: //Statement /statements to be executed break; case value'n': //Statement /statements to be executed break; // There can be as many Cases as the user wants in a Switch block //default is an optional case and executed if none of the case values matches the expression default: //statement }
The above flow diagram clearly shows how the Switch and Case statement works in Java. It shows how matching the expression defined in the Switch statement is matched with the Case value starting from the top until the last steps. If the value is not matched until the last step, i.e. false’ is returned at the end of every case, then the code inside the ‘default’ is executed. If the value of any case is matched with the expression, i.e. return ‘true’ in any of the Case statements, then the code of a particular block is executed, and then execution exits the Switch block.
The below examples clearly show how the Case statement work in Java.
When the value of the Switch expression is matched with a Case value
Code:
public class MyClass { public static void main(String args[]) { int value = 8; switch (value) { case 3: System.out.println("Congratulations here is the case 3 executed"); break; case 4: System.out.println("Congratulations here is the case 4 executed"); break; case 8: System.out.println("Congratulations here is the case 8 executed"); break; default: System.out.println("Sorry none of your cases matched"); break; } } }
Output:
When no value is matched with the Switch expression
Code:
public class MonthClass { public static void main(String args[]) { int month= 13; switch (month) { case 1: System.out.println("Month is January"); break; case 2: System.out.println("Month is February"); break; case 4: System.out.println("Month is April"); break; default: System.out.println("Sorry either the value you have given is invalid or not matched with any of the case."); break; } } }
Output:
When there is a missing break keyword in Case statements
Code:
public class VowelClass{ public static void main(String args[]) { char ch = 'a'; switch (ch) { case 'e': System.out.println("Value matched - e, a vowel\n"); break; case 'a': System.out.println("Value matched - a, a vowel"); case 'w': System.out.println("Value matched - w, not a vowel"); case 'o': System.out.println("Value matched - o, a vowel"); default: System.out.println("Sorry none of your cases matched"); break; } } }
Output:
When there is no default block in the switch block
Code:
public class NoOutputClass { public static void main(String args[]) { char grades = 'D'; switch (grades) { Case 'A': System.out.println("Grade A - Excellent"); break; case 'B': System.out.println("Grade B - Good"); break; case 'C': System.out.println("Grade C - Poor"); break; } } }
Output:
In the above article, we have mentioned almost all the scenarios of the switch statements and the outputs that they can generate. Though it is very easy to work on Switch statements but it should be understood thoroughly by the programmer before working on them as sometimes it can produce unexpected results if some mistakes are done. Normally Switch should be used in a scenario where there is a need to perform the action on certain conditions, and conditions are many. In the case of only 2-3 conditions, things can be worked out with if-else -if statements.
This has been a guide to Case Statement in Java. Here we discuss the Flow diagram of the Switch-Case statement in Java along with Syntax with Examples. You can also go through our other suggested articles to learn more –
The above is the detailed content of Case Statement in Java. For more information, please follow other related articles on the PHP Chinese website!