Java If statement
The Java control statements are divided into three categories: ① sequence structure, ② selection structure, ③ cyclic structure.
--------------------------------------------------------------------------------
The selection structure is divided into: ① single selection structure, ② double choice structure, ③ multiple choice structure.
Mainly involved: if_else, switch, while, break and continue, for.
If single selection structure
Test the conditional expression once, and if the test is true, execute the following statement or skip the statement.
Example (use of the math class)
Random (), returns the decimal number of a positive symbol, and the interval size is [0,1).
Note the problem with the scope of if: If statement is not added {}, its control scope is limited to the first sentence. (General suggestion Plus {}, develop a good habit)
If-else Dual Selection structure
When the conditional expression is true, execute the statement block 1, otherwise, execute the statement block 2, which is the else part.
If-else If-else Multi-selection structure
Let me introduce you to the Java switch statement
Java Control Statement--switch statement
The equivalent judgment of the above if statement can be replaced by a switch.
Note that after each case, you typically add a break to indicate that the current case is finished; prevent the case from penetrating, that is, continue to execute the case until it encounters a break.
The following example in turn exploits the case penetration phenomenon.
Example
JDK7.0 new features: Enhanced switch
Before JDK7, the switch (expression) {...}, where the expression result can only be int (the Byte, short, char) that can be automatically converted to int, enumerated types.
But in JDK7, the result of an expression can also be a string.
(PS: Automatic type conversion: A data type with small capacity can be automatically converted to a large capacity data type.) Byte (1 bytes) →short (2 bytes) →int (4 bytes). )
The above is a small set to share the Java control statements If, switch statements, I hope to help.