Java05-java Basic Grammar (iv) cyclic structure
Loop structure (repetition/iteration): Repeating partial statements based on conditions
1. While loop structure
while (conditional expression) {
loop body statement;
}
1) Syntax: A, while is a keyword
B, "conditional expression" must be enclosed in parentheses.
C, "{}" means the control range of the loop, if there is no "{}", while the control range to the first semicolon thereafter;
2) Execution Process:
(1) operation "conditional expression", if the result is true, transfer to (2); If the result is false, transfer to (3)
(2) Execute once, loop body statement, transfer (1)
(3) Exit cycle
2. Do-The While loop:
do{
loop body statement;
}while (conditional expression);
1) Syntax:
A, do is a keyword and must be used in conjunction with while and cannot be used alone
The semicolon after the B, while (conditional expression) cannot be omitted, indicating the end of the loop
2) Execution Process:
(1) Execute "loop body statement" first
(2) Run "conditional expression", if true, transfer to (1); If False, transfer to (3)
(3) Exit cycle
Emphasis: Two types of cycle differences:
1) The While loop is first judged to be true and then executes the loop body statement; The loop body statement is executed at least once, regardless of whether the condition is true or do-while.
2) While loop "conditional expression" does not have a semicolon after the do-while loop "conditional expression" must have a loop.
3. For loop:
for (expression 1; expression 2; expression 3) {
loop body statement;
}
1) Syntax:
(1) for is a keyword
(2) Three expressions can be omitted partially or completely, but semicolons cannot be omitted
Expression 1: Assigning an initial value to a variable
Expression 2: Conditional expression, controlling whether the loop continues
Expression 3: The statement that the loop tends to end
2) Execution Process:
(1) First operation "Expression 1"
(2) operation "Expression 2", if True, into (3), if False, transfer (4)
(3) Execute "loop body statement", operation "Expression 3", transfer to (2)
(4) Exit cycle
4, for Loop, while loop, do-while cycle between transitions
(1) For loop
for (expression 1; expression 2; expression 3) {
loop body statement;
}
(2) While loop
Expression 1;
while (expression 2) {
loop body statement;
Expression 3;
}
(3) Do-while cycle
Expression 1;
do{
loop body statement;
Expression 3;
}while (expression 2);
Java05-java Basic Grammar (iv) cyclic structure