The switch statement super detailed Oh ~

Switch statement format:
Switch (expression) {
case value of 1:
the sequence of statements 1;
[BREAK]; // met [] denotes generally may or may not generally recommended to use
the values of case 2:
the sequence of statements 2;
[BREAK];
...
[default]:
default statement;
BREAK;
value calculation expression ####. And its value by one after constant expression compared to when the value of the expression is equal to the value of a constant expression, i.e., executing a subsequent statement, and then no longer determined, continue statements after the rear case. The value of the expression with the constant expressions are not all the same case, the default statement is executed. Add this statement after the break, once the value of the "constant expression x" and "expression" is equal, then the implementation of "statement x", after the implementation, thanks to the direct break out of the switch statement, the switch statement continues behind the program, so you can avoid performing unnecessary statement.
The following code:
public class TestSwitch {
public static void main (String [] args) {
int month The = (int) (* 12 is Math.random. 1 + ());
System.out.println ( "Month:" + month); + // here is the role of string concatenation, "month:" and the following month together will be more intuitive. If the direct print a month, we look at print information not see the value of the month is the month.
switch (month) {
1 Case:
System.out.println ( "! January the New Year!");
BREAK;
Case 2:
System.out.println ( "! February spring up!");
BREAK;
default:
System.out.println ( "I am the other months!");
}
}
}
matters switch statement to note:

  1. Variable switch statement can only be used by byte, char, short, int, String data type, String data type is time to start from jdk7.0 support.
  2. With the latter case the data must be a constant.
  3. switch stop conditions:
    a switch statement once a match on one of the case statement, the statement will execute the code corresponding to the case of, if not finished after
    experiencing the break keyword or the end of the switch statement braces, then switch statement will not be judged in the order of execution of code from the top
    all the code. Hard against the break or the end siwitch statement braces.
    4. In a switch statement, regardless of the order code, always will first determine the case statement, and no default statement will be implemented in line with the case.

Guess you like

Origin blog.csdn.net/qq_33570145/article/details/94201388