Java Switch Case
# Java switch case Statement
The switch case statement checks whether a variable is equal to one of a series of values. Each value is called 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 caseStatements default : //Optional //Statements}
!(#)
The switch case statement has the following rules:
* The variable type in a switch statement can be: 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 the value to be compared 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 a literal constant.
* When the value of the variable matches the value of a case statement, the statements after 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. The program jumps to the statement after the switch statement. Case statements do not necessarily need to contain a break statement. If there is no break statement, the program will continue to execute the next case statement until a break statement is encountered.
* A switch statement can contain a default branch, which is generally the last branch of the switch statement (it can be in any position, but it is recommended to be last). The default branch is executed when none of the case statement values match the variable value. The default branch does not require a break statement.
**When a switch case is executed, it will always match first. If the match is successful, it returns the value of the current case. Then, based on whether there is a break, it determines whether to continue outputting or to exit the judgment.**
### Example
## Test.java File Code:
public class Test{public static void main(String args[]){//char grade = args.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 compilation and execution result of the above code is as follows:
Good, your grade is C
If there is no break statement in the case block, the JVM will not sequentially output the return value corresponding to each case. Instead, it continues matching. If the match is unsuccessful, 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 compilation and execution result of the above code is as follows:
default
If there is no break statement in the case block, after a successful match, starting from the current case, the values of all subsequent cases 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:
12default
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 judgment.
## 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:
123
YouTip