Switch Case in Java

Java
Java

When it comes to web development, Java is the first choice for a majority of developers across the globe. Java continues to be one of the most famous enterprise coding languages.

According to a report by PopularitY of Programming Language or PYPL index released in February 2022, the usage of Java has increased by around 1.2% and is at second position in the list of most popular programming languages across the world.

Switch case, an important part of Java, is widely used for many applications while coding. If you want to know what is a switch case and the way it is used in Java, read below to find it out.

The switch statement is simply a multi-way branching statement. In other words, a Java switch statement can execute a single statement from multiple conditions. It works similar to an if-else-if statement. On the basis of the value provided to the expression given, various parts of the code can be executed quickly and easily. This expression can be in the form of primitive data types such as char, int, byte, and short. The switch case in Java also works with enumerated data types (enum), string, and wrapper classes with JDK7.

Using Switch Case in Java

The switch case in Java is typically used to choose one of many code blocks that are to be executed.

Default Keyword: this keyword is used to mention the code executed when the given expression does not match any test case.

Break Keyword: as Java encounters a Break keyword, the control breaks and steps out of the switch block. This means that the execution of the block stops on reaching the Break keyword and if the match is found, the case testing ceases there. On reaching a matching test case, the control comes out of the block, thereby saving a lot of time that is otherwise wasted in testing the other cases.

Working of Switch Statement

Switch case is used mainly used for its capability of checking multiple conditions at once. It is provided with an expression that may be a literal expression or a constant that is to be evaluated. The value of the expression is compared to each test case until a matching test case is reached. And if the expression doesn’t match any value, the code associated with the default keyword is executed. If the value matches a test case, the respective code is executed.

There are some rules that you need to follow while using the switch statement in Java. They are discussed below.

Rules for declaring Switch Statement 

  • The same value cannot be assigned to two cases
  • In the switch statement, the data types of each test case should be the same
  • For all the cases, the value should be in the form of a literal or a constant, but not a variable
  • When you need to stop the statement sequence inside the switch statement, you use the break keyword
  • You can assign the value to the test cases in any order on the basis of your requirements. It doesn’t need to be in a specified order (ascending or descending)
  • If you exclude the break statement, the execution of the statement will continue to the next statement even if the required match is found
  • You can write the default statement anywhere in the switch block, but if it is not at the end, it should be followed by a break statement
  • You can also do nesting with a switch statement, but it makes your program more complex

Syntax of Switch Case

The syntax of the switch case is given below:

switch(expression)

{

 

//There are case statements in the switch block that need to be of the same type of expressions

 

Case value1:

 

//code statement that is to be executed

 

break; //optional keyword

 

case value2:

 

// code statement that is to be executed

 

break; //optional keyword

 

//there can be more case statements, as per your requirements

 

// when there is no matching case value, a default statement is used, and no break is required in the default case

 

default:

 

//code statement that is to be executed

 

}

 

For Example, the code below will tell you the color of the rainbow when you put the number as input.

 

public class Main {

public static void main(String[] args) {

int color = 3;

switch (color) {

 

case 1:

System.out.println(“Violet”);

break;

 

case 2:

System.out.println(“Indigo”);

break;

 

case 3:

System.out.println(“Blue”);

break;

 

case 4:

System.out.println(“Green”);

break;

 

case 5:

System.out.println(“Yellow”);

break;

 

               case 6:

System.out.println(“Orange”);

break;

 

case 7:

System.out.println(“Red”);

break;

 

 

default: System.out.println(“invalid color”);

}

}

}

Output: Blue

Executing Switch statement excluding the Break statement

Since it is not essential to use the break statement, we can exclude it. When you don’t use the break statement, the control continues to check with the following cases. You can use multiple cases without a break statement, which is required in many cases.

For example, the code below shows whether a month has 28, 30, or 31 days and demonstrates a switch case with multiple statements between the break statements.

public class Main {

public static void main(String[] args) {

int month = 3;

switch (month) {

 

case 2: System.out.println(“28 Days”);

break;

 

case 1:

 

case 3:

 

case 5:

 

case 7:

 

case 8:

 

case 10:

 

case 12:

 

System.out.println(“31 Days”);

 

break;

 

 

case 4:

 

case 6:

 

case 9:

 

case 11:

 

System.out.println(“30 Days”);

 

break;

 

}

}

}

 

 

Output: 31 Days

 Apart from these examples, you can use a Switch statement with nesting as well. This implies a nested switch case where a switch case can be executed as a part of an outer switch statement block.

Conclusion

This article gives you just a simple illustration of a switch statement; the actual power of this statement is yet to be described. If you wish to explore more about switch statement and their use cases in different scenarios and the way it provides you with efficiency along with saving a lot of time, Simplilearn is there to help you out.

Enroll yourself in an online training course from Simplilearn and go through the actual essence of the Switch statement.

Read More: Can a hit-and-run be charged as a felony in California?

Leave a Reply

Your email address will not be published.