Java

How to Use Switch Case Statement in Java

In Java, the switch statement allows users to implement compact, concise, clear, and readable code. It is one of the strongest and most powerful programming statement as compared to if-else. If the number of cases is limited, we can use an if-else statement. However, if the case numbers are large in size, it is preferred to use the switch case statement.

This tutorial will talk about using the switch statement in Java.

How to Use Switch Case Statements in Java?

The switch statement is a various condition statement, like if, else if. It executes only one statement from all specified condition code blocks. It deals with enums, strings, int, short, long, byte, and many others. To utilize the switch statement in Java, we have provided the below syntax.

Syntax

switch(expression){
 case value1:
 break;
 case value2:
 break;
......
 default:
}

In the above syntax:

  • switch” is an expression that is executed only once.
  • case” determines the condition. The value of the stated expression is compared with each case.
  • break” is an optional keyword used to terminate the condition.
  • default” case is executed when the defined condition is not matched.

Example

In this stated example, we will utilize the switch case statement to compare the condition. To do so, first of all, declare a number with numeric data type and assign the value according to your specification:

int num=23;

Here:

  • Utilize the switch statement and add conditions with the help of the “case” keyword.
  • Then, use the “println()” method to print the output on the console if the number matches the stated condition.
  • Furthermore, the default statement is optional. If the number does not match with any condition, then it will print the default value:
switch(num){
 case 1: System.out.println("15");
 break;
 case 2: System.out.println("25");
 break;
 case 3: System.out.println("35");
 break;
 default:System.out.println("Not Exist");
}

In the below image, it can be noticed that the declared number does not match with any condition. That is why it will print the default value on the console:

Let’s see another example of switch case statements. To do so, initialize the variable:

int day = 5;

Use the switch statement that will compare the number with each case statement. If the number matches with any condition, it will terminate and print the output on the display. In other case, the default value will be printed on console:

switch (day) {
 case 0:
  System.out.println("Today is Monday");
  break;
 case 1:
  System.out.println("Today is Tuesday");
  break;
 case 2:
  System.out.println("Today is Wednesday");
  break;
 case 3:
  System.out.println("Today is Thursday");
  break;
 case 4:
   System.out.println("Today is Friday");
  break;
 case 5:
   System.out.println("Today is Saturday");
  break;
 case 6:
   System.out.println("Today is Sunday");
  Break;
}

As the specified day value has been matched with the “5” case, its corresponding code block is executed.

Conclusion

To use the switch case statement in Java, first, initialize the variable with data type and assign the value. Then, utilize the switch case statement that compares the number with each case. If the number matches the condition then the number is displayed on the console screen. This post stated the method for using the switch case statement in Java.

About the author

Hafsa Javed