Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Case Statement in Java

What is the Case Statement in Java?

While programming, it is very difficult to handle when it comes to deal with different actions on different conditions. Though in Java and many other programming languages, statements like if, if-else, if-else-if are used in case of 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 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 a 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.

How Does Case Statement work in Java?

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 with Case values then, the default statement defined in the Switch block is executed otherwise nothing got executed.

Important points to summarise:

  • There can be multiple switch blocks in the program depending on the different conditions.
  • There can be any number of Case statements in a single Switch block.
  • Duplicate values in the Case statements are not allowed.
  • The data type of variable of switch statement needs to be the same as the Case statement value.
  • Variables are not allowed for the Case value. Value can be constant or literal.
  • Break keyword in each Case is used to terminate that particular sequence of statements of that case. If the break is not used in any of the case statements then there would be no error instead all the cases proceeding the matching case will get executed.
  • Beak and default keywords are optional in Switch blocks.

Syntax of Switch Case Statement in Java

switch (expression)
{
// case statements
// same data type for switch expression and case value Case value1:
//Statement /statements to be executed
break;
Case value2:
//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
}

Flow Diagram of Switch Case Statement

The above flow diagram clearly shows how the Switch and Case statement works in Java. It shows how the matching of 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, ie 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 particular block is executed and then execution exits the Switch block.

Examples of Case Statement in Java

The below examples clearly show how the Case statement work in Java.

Example #1

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:

Example #2

When no value is matched with 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:

Example #3

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:

Example #4

When there is no default block in 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:

Conclusion

In this above article, we have mentioned almost all the scenarios of the switch statements and the outputs that can be generated by them. 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.

Recommended Article

This has been a guide to Case Statement in Java. Here we discuss the Flow diagram of Switch-Case statement in Java along with Syntax with Examples. You can also go through our other suggested articles to learn more –

  1. Star Patterns in Java
  2. Java Tools
  3. Frameworks In Java
  4. Best Java Compilers

The post Case Statement in Java appeared first on EDUCBA.



This post first appeared on Free Online CFA Calculator Training Course | EduCB, please read the originial post: here

Share the post

Case Statement in Java

×

Subscribe to Free Online Cfa Calculator Training Course | Educb

Get updates delivered right to your inbox!

Thank you for your subscription

×