Switch structure can better solve the problem of equivalence judgment
Switch Select the syntax for the structure:
switch (expression) {
CASE constant 1:
code block 1;
Break
Case constant 2:
code block 2;
Break
。。。。。。
Default
code block N;
Break
}
Explain:
1.switch: means "switch", which is the value in the parentheses behind the switch.
Enclose an shaping variable (an expression) or a character type (expression) in parentheses.
2.case: "Condition." Case must be a constant of integer or character type,
Usually a fixed character, a number, such as "8". " A ". Case blocks can have more than one,
The order can be changed, but the values of the constants after each case must be different.
3.default: Indicates "Default", that is, other cases are not satisfied. Follow the colon immediately after default.
The order of the default block and the next case block can be changed without affecting the results of the program execution.
Usually, the default block is placed at the end, or it can be omitted.
4.break means "Stop", that is, jump out of the current structure.
Switch select the execution process of the structure:
Calculate and get the value of the expression or variable in parentheses after the switch, and then
Compare the order of the calculations with the constants after each case, and execute the code in the block when the second is equal;
When a break is encountered, jump out of the switch selection structure and execute the code after the switch selection structure.
If there is no one case after the constant is equal to the value in the parentheses after the switch, the execution
The code in the default block at the end of the switch.
Attention:
1. Each case code block can have more than one statement, that is, you can have a set of statements, and do not need to be enclosed in "{}".
Both case and default have a colon that cannot be omitted, otherwise the compilation does not pass. For the end of each case,
Think about whether you need to jump out of the switch structure from here. If necessary, do not forget to write "break";.
2. In the code block following the case, when the value of the variable day is 1 2 3 o'clock, the output is "odd!" ”;
When the value of the variable day is 2 4 6 o'clock, it will output "even!" ”
eg
int num=3;
Switch (num) {
Case 1:
Case 3:
Case 5:
SYSTEM.OUT.PRINTLN ("Odd! ");
Break
Case 2:
Case 4:
Case 6:
SYSTEM.OUT.PRINTLN ("Even!") ")
Break
}
The multiple if and switch selection structures are similar, and they are all structures used to handle multi-branch conditions.
However, the switch selection structure can only be used in the case of equivalent condition judgment.
In versions prior to JDK 1.6, the condition of the switch selection structure must be an integer variable or a character variable.
In JDK 1.7, however, you can also use the string type in the condition of the switch selection structure.
In the process of program development, it is necessary to consider the high fault tolerance of the program.
Switch vs. Java