Java switch case Statement
The switch case statement is used to determine if a variable matches any value in a series of values, with each value representing a branch.
Syntax
The syntax for the switch case statement is as follows:
switch(expression){
case value :
// Statements
break; // Optional
case value :
// Statements
break; // Optional
// You can have any number of case statements
default : // Optional
// Statements
}
The switch case statement has the following rules:
The variable in the switch statement can be of type byte, short, int, or char. Starting from Java SE 7, switch supports the String type, and the case labels must be string constants or literals.
A switch statement can have multiple case statements. Each case is followed by a value to compare and a colon.
The data type of the value in the case statement must be the same as the data type of the variable, and it can only be a constant or literal constant.
When the value of the variable matches the value in a case statement, the statements following that case are executed until a break statement is encountered, which exits the switch statement.
When a break statement is encountered, the switch statement terminates, and the program continues execution at the statement following the switch statement. The case statement does not necessarily need to include a break statement. If no break statement is encountered, the program will continue to execute the next case statement until a break statement is found.
A switch statement can include a default branch, which is typically the last branch of the switch statement (though it can be placed anywhere, it is recommended to be the last). The default branch executes when no case statement value matches the variable value. The default branch does not require a break statement.
When executing a switch case, it always performs a match first. If a match is successful, it returns the current case value, and then determines whether to continue output based on the presence of a break statement.
Example
Test.java File Code:
public class Test {
public static void main(String args[]){
// char grade = args[0].charAt(0);
char grade = 'C';
switch(grade)
{
case 'A' :
System.out.println("Excellent");
break;
case 'B' :
case 'C' :
System.out.println("Good");
break;
case 'D' :
System.out.println("Pass");
break;
case 'F' :
System.out.println("You need to work harder");
break;
default :
System.out.println("Unknown grade");
}
System.out.println("Your grade is " + grade);
}
}
The above code compiles and runs with the following result:
Good
Your grade is C
If there is no break statement in the case block, the JVM does not sequentially output the return values corresponding to each case. Instead, it continues to match, and if no match is found, it returns the default case.
Test.java File Code:
public class Test {
public static void main(String args[]){
int i = 5;
switch(i){
case 0:
System.out.println("0");
case 1:
System.out.println("1");
case 2:
System.out.println("2");
default:
System.out.println("default");
}
}
}
The above code compiles and runs with the following result:
default
If there is no break statement in the matched case block, after a successful match, the values of all subsequent cases starting from the current case will be output.
Test.java File Code:
public class Test {
public static void main(String args[]){
int i = 1;
switch(i){
case 0:
System.out.println("0");
case 1:
System.out.println("1");
case 2:
System.out.println("2");
default:
System.out.println("default");
}
}
}
The compilation and execution result of the above code is as follows:
1
2
default
If the currently matched case block does not have a break statement, then starting from the current case, the values of all subsequent cases will be output. If a subsequent case block has a break statement, it will exit the switch statement.
Test.java File Code:
public class Test {
public static void main(String args[]){
int i = 1;
switch(i){
case 0:
System.out.println("0");
case 1:
System.out.println("1");
case 2:
System.out.println("2");
case 3:
System.out.println("3"); break;
default:
System.out.println("default");
}
}
}
The compilation and execution result of the above code is as follows:
1
2
3