Home > Java > javaTutorial > Case Statement in Java

Case Statement in Java

WBOY
Release: 2024-08-30 15:24:19
Original
1164 people have browsed it

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

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 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 case statements, then there would be no error; instead, all the cases proceeding with the matching case will be 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 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
}
Copy after login

Flow Diagram of Switch Case 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.

Case Statement in Java

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;
}
}
}
Copy after login

Output:

Case Statement in Java

Example #2

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;
}
}
}
Copy after login

Output:

Case Statement in Java

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;
}
}
}
Copy after login

Output:

Case Statement in Java

Example #4

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;
}
}
}
Copy after login

Output:

Case Statement in Java

Conclusion

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.

Recommended Article

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 –

  1. Star Patterns in Java
  2. Frameworks In Java
  3. Best Java Compilers
  4. Case Statement in JavaScript

The above is the detailed content of Case Statement in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template