
We use Java Switch case when we have multiple options and we have to choose only one option from the available options. You can perform particular task depending on the selected option.
The switch case statement is an alternative to multiple if-else conditions, because multiple if-statements are a very time-consuming process.
A switch statement works with byte, short, char and int primitive data types. It also works with enumerated types and String classes.
Table of Contents
Syntax of Java Switch Case
This is the syntax of the java switch statement
switch (variable) {
case value1:
// Code block to be executed if expression is equal to value1
break;
case value2:
// Code block to be executed if expression is equal to value2
break;
case value3:
// Code block to be executed if expression is equal to value3
break;
default:
// Default statement
}
The particular code block will execute based on the value of the variable. If the value of variable is equal to value1, then code block of value1 will execute. And If the value of variable is equal to value2 then the code block of value2 will execute and so on.
If the value of the variable is not equal to value1, value2, etc., the program control goes to the default section and the code block of the default statement will execute.
How does the switch-case statement work?
Take a look at the switch case syntax above. Switch is a variable within a parameter, and we’ve used value1, value2, and value3 with case.
- Each case’s values (value 1, value 2, and value 3) are compared to the value of variable.
- The code for case value1 is run if the variable has the value1. In the same way, if the variable matches the value2, the case value2 code is run and so on.
- If no match is found, the code for the default case is used.
Example of Java Switch case
public class SwitchDemo {
public static void main(String[] args) {
int day = 1;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Invalid day");
}
}
}
We mostly use switch statements in menu driven programs where we show a list of items and the user can select an item from the list of items available in the menu.
What is the use of break keyword in switch?
In java program we use the break statement inside the switch blocks to come out of the switch block. Break statements are necessary because without them, the statements in the switch block fall through. The break statement ignores the execution of other code in the switch block.
What is the use of default keyword in switch?
In switch statement, there may be possibility that some section switch case does not handles, in this case the default statement handles that section.
We can use default statement at any place inside the switch but we should use in the last statement in a switch block. Default statement does not need a break.
Java Switch fall-through example
Let’s take a look at the fall-through program, when we don’t use break statement in some cases, so that you can understand better about fall-through
public class SwitchDemo {
public static void main(String[] args) {
int day = 1;
switch (day) {
case 1:
System.out.println("Monday");
case 2:
System.out.println("Tuesday");
case 3:
System.out.println("Wednesday");
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Invalid day");
}
}
}
Output:
Monday
Tuesday
Wednesday
Thursday
In conclusion, You have learned about the switch statement, syntax of switch statement, java switch fall-through and the example.
Switch statement is a very important section in Java programming for beginners and experience, for interview purpose.
Recommended Articles
- List of programs in Java
- Thread Life cycle and Thread creation in Java
- Simple example of thread synchronization in java
- When to use java streams?
- Find String List from List of Objects using Java 8 Streams
- Functional interfaces and lambda expressions