1. Syntax: if (condition 1) {
Code 1
}else if (condition 2) {
Code Block 2
}else{
Code block 3
}
else if: No, can be multiple
Else: Can not, at most one, and can only be placed in the last
First Judge condition 1, when the result is true, execute the code block 1 inside the curly brace, jump out of the structure!
When the result is false, then the condition 2 is judged, when the result is true, the code block 2 inside the curly brace is executed, when the structure jumps out!
If the condition is not true, execute the code block in the else curly brace 3
Note: The condition in parentheses after the If must be a Boolean expression, that is, the value of the expression must be a Boolean value
Braces in the structure try not to omit
2. Nested IF selection structure
if (condition 1) {
Code Block 1
if (condition 2) {
Code Block 2
}else if (condition 3) {
Code block 3
}else{
Code Block 4
}
}else{
Code block 5
}
The condition of the inner if is judged only if the condition of the outer if is satisfied.
3.SWITCH selection Structure
Advantages: Switch selection structure can better solve the problem of equivalence judgment
Switch (variable) {
CASE constant 1:
code block 1;
Break
Case constant 2:
code block 2;
Break
Default
code block 3;
Break
}
Note that the value in parentheses after 1 switch must be short,int,byte or character or string
Note the 2 case must be followed by a constant expression that is either shaped or character, and the constant value after each case must be different.
Note 3 case with the default keyword is followed by ":", not a semicolon.
Note 4 in general, after the end of the code block behind each case, there must be a break, which represents jumping out of the current structure.
Note 5 Case does not have curly braces behind it.
4.while (loop condition) {
Looping operations
}
Looping features:
Loop variable initialization (from what start loop)
Loop conditions (conditions that enter the loop to perform the loop operation)
Circular operation (Loop specific things to do)
Cyclic variable update (the loop variable needs to be changed to jump out of the loop)
Features: first judgment, after execution
Steps:
A analysis of cyclic conditions and cyclic operations
B writing code with while syntax
C Check whether the loop exits
5.do {
Looping operations
} while (loop condition);
Features: At least once, then judge
6. Different points:
Different syntax
Different execution order
Number of executions
7.for (initialization; condition; Iteration statement) {
Loop body
}
Features: first judgment, after execution
Execution process: First executes the initialization part, then carries on the condition judgment, then executes the loop body, finally carries on the iteration part computation.
Judge again, if the condition is not set, jump out of the loop.
Note: Variables defined in the For loop are local variables and cannot be used outside of loops
8. Generate Random Numbers
Math.random ()//Generate a 0.0~1.0 double decimal
9. Comparing strings
String s= "a";
S.equals ("a"); Finally, a Boolean value is obtained
Jen Baby Java Basic Notes Flow control (i)