1. Conditional statements
(1) If statement (single condition)
if (expression) {
EXECUTE statement block
}
(2) execution Process
·
If statement
A conditional expression can be any type of logical expression
If the expression value is true, the contents of the curly brace are executed before the following statement is executed
If the expressed value is false, the following statement is executed directly
If there is only one statement in the curly brace number, you can omit the curly braces (it is recommended that you do not
Omitted
Variable = Boolean expression? Statement 1: Statement 2
If the value of the Boolean expression is true, the statement 1 is executed
If the value of the Boolean expression is false, the statement 2 is executed
If statement (multiple selection one)
if (expression 1)
{
Executing a statement block 1
}
else if (expression 2)
{
Executing a statement block 2
}
Switch statement
switch (expression)
{
Case value 1: statement block 1
...
Case Value N: statement block n
Default: statement block n+1
}
Switch statement
The value of an expression can only accept int, Byte, char, short, and no other type of value
Duplicate case values are not allowed
Once the switch encounters the first case match, the program jumps to the tab position and starts executing all the program code in the future, regardless of whether the following case conditions match, until the break statement is reached
2. Circular statements
While loop
while (conditional expression) {
EXECUTE statement block
}
Do...while Cycle
Do
{
EXECUTE statement block
} while (conditional expression);
For loop
for (initialization expression; cyclic conditional expression; post-loop action expression)
{
EXECUTE statement block
}
Dead loop
for (int x=1;; X + +) {
System.out.println ("x=" +x);
}
while (true) {
System.out.println ("x=" +x);
}
Interruption of the Loop
When you use a loop statement, you can end the loop only if the value of the loop condition expression is false. Sometimes we want to interrupt the loop prematurely, to do this, simply add a break or continue statement to the LOOP statement block
Break End a recent layer of loops
Continue; jump out of this cycle
Process Control Statements
for (①;②;④) {
③
}
Enhanced for Loop (array loop)
for (int i:array) {
System.out.println (i);
}
Java Chapter III Flow control statements