In this section we will learn what the switch statement is and how to use it in Java.
What Is switch Statement in Java?
There’s a value in a variable and you don’t know what that value is but you have multiple guesses about what the value might be. Also for each value you have a block of code ready to be executed.
This scenario is a perfect candidate for the use of the switch statement!
Basically the switch statement takes an input. Within the body of this statement we can define different values and a block of code for each of these values. Now the input is compared with the already defined values in the body of the switch statement. At the end, the body of any value that matches the input is the one to be executed.
For example, someone asked us to write a program that checks an integer value; if that value is 1, we need to send the “Hello” message to the output stream. If the value is 2 we need to send the “Good Morning” message to the output stream, and if the value is 3 then we should send the “Good Bye” message to the output stream.
This is basically a situation where we can use the switch statement.
Syntax of Switch Case:
Before writing the program, let's go and see the structure of a switch statement
switch (expression){
case value1:
//body…
case value2:
//body…
case value_N:
//body…
default:
//body…
}
`switch` keyword: to create a switch statement, we start with the keyword `switch`.
( ): after the keyword switch comes the parentheses () and inside it we set the input value that we want to check.
Note: only integer values like char, int, short, long and String values as well as objects are allowed as the value of the switch statement. But we can’t use floating values of type double and float (like 2.3, 44.32 etc.)
`{ }`: the opening and closing braces declare the body of the switch statement.
case block in switch
Each case statement is a checkpoint where we put a value. This value then will be checked against the input (that is set within the parentheses of the switch statement) and if they matched, the body of that case statement will run.
Also the body of a case statement starts after the colon : and ends right before the next case statement or the default statement.
Note:
-
Depending on the design of the switch statement, we can put one
or more `case` statement.
Java `break` statement in switch
Within the body of each case statement we need to use either break or return statement to end the execution of the body of the switch statement and jump out.
If we use the break statement, the CPU will run the instructions after the body of the switch statement (it means still in the same method that the switch statement is).
But if we use the return statement instead, that means return from not just the body of the switch statement but also from the method that this statement is in.
Note: in later sections we explained more about break and return keywords.
Java `default` statement in switch
The use of default statement is like using else statement in the if-else. The body of this statement will only run if none of the case statements match the input value.
Note: we use the default statement as the last statement in the body of a switch.
Java switch statement example:
public class Simple {
public static void main(String[] args) {
int iVal = 3;
switch (iVal){
case 1:
System.out.println("Hello");
break;
case 2:
System.out.println("Good Morning");
break;
case 3:
System.out.println("Good Bye");
break;
default:
System.out.println("unknown value");
break;
}
}
}
Output:
Good Bye
How does the switch statement work?
In this example, we used the value of the iVal variable as the value of the switch statement. (Note: the data-type of this variable is int so it can be used for the switch statement)
Within the body of this statement, we first started with the case 1 and here the value of this case statement will be checked against the input value. But because these two values are not equal, the body of this statement will be ignored and the next case statement is the one to be checked next.
The second case statement also has the value 2 and is not a match for the input value, and so this statement is also rejected.
The third case statement has the value 3 and it is actually equal with the input value. So the body of this statement will run.
Then inside the body of this third statement, we sent the message “Good Bye” to the output stream and after that, we called the break statement to break the body of the switch statement and move on to those instructions after this body.
Note: for this particular example, there’s no instruction after the body of the switch statement and so after the call to the break statement, the main method reaches the end of its instructions and so terminates.
Also as you can see the body of the default statement in the example above didn’t run because the third case statement matched the input value and so there’s no need to run the body of the default.
Java Switch Multiple Case
We can also put multiple case statements under each other and define only one body for them.
Example: creating switch with multiple case in Java
public class Simple {
public static void main(String[] args) {
int value = 3;
switch (value){
case 1:
case 2:
case 3:
System.out.println(" The input value is in range of 1-3 inclusive ");
break;
case 4:
case 5:
case 6:
System.out.println(" The input value is in range of 4-6 inclusive ");
break;
default:
System.out.println("The range of the input value is unknown");
break;
}
}
}
Output: The input value is in range of 1-3 inclusive
In this example the first 3 case statements have one body and any of these cases that matches the input, will run this body. Also the last 3 case statements have only one body.
Omitting the `break` statement in Switch
When working with switch statement, it is important to call the break statement within the body of each case statement as well.
If we don’t break a switch statement when a case matched the requirement, the body of the remaining case statements will be executed as well without any condition check!
Example: omitting the `break` statement in Switch
class Main{
public static void main(String[] args) {
String name = "John";
switch(name){
case "John":
System.out.println("The value is John");
case "Omid":
System.out.println("The value is Omid");
case "Jack":
System.out.println("The value is Jack");
default:
System.out.println("The value is unknown");
}
}
}
Output:
The value is John
The value is Omid
The value is Jack
The value is unknown
In this example, the first case block matched the value of the switch statement and so its body is executed. But then because we didn’t break the switch in this block, the rest of case statements were also executed.
So remember, use the break statement at the end of each case block in order to end the work of a switch and move on with the rest of program.
Nested switch statement in Java
Within the body of a switch statement and in its case blocks we can use other switch statements as well.
This is called nested switch statement.
Example: Nested switch statement
class Main{
public static void main(String[] args) {
String name = "John";
String name2 = "Omid";
switch(name){
case "John":
System.out.println("The value is John");
switch(name2){
case "Jack":
System.out.println("The value of the name2 is Jack");
break;
case "Omid":
System.out.println("The value of the name2 is Omid");
break;
default:
System.out.println("The value of the name2 is unknown");
}
break;
case "Omid":
System.out.println("The value is Omid");
break;
case "Jack":
System.out.println("The value is Jack");
break;
default:
System.out.println("The value is unknown");
}
}
}
Output:
The value is John
The value of the name2 is Omid
This is a dummy example, but it shows that there’s no limit on what we can put in the body of a case block!