The Java program structure is divided into sequential structure, selection structure, and cyclic structure according to the idea of structured program.
① SELECT statement
The selection structure is divided into single selection, double selection and multiple selection. The double choice is the standard choice structure, the single choice is the simplification form of the double choice, the multiple choice is the double choice nesting form.
Many times, you need to use the nesting of the selection structure, when nesting, pay attention to using curly braces to clear the nesting level, otherwise it will cause the logic confusion, because the editor will be the other closest to its nearest if pairing.
The format of the switch statement:
1 Switch(expression)2 {3 CaseValue 1: statement block 1; Break; 4 CaseValue 2: statement block 2; Break; 5 CaseValue 3: statement block 3; Break; 6 ... ..7 default: statement block; 8}
Description
(1) The value of an expression must be integer or character data, and be the same as the constant value type after the case in each statement. The value of an expression is only compared to the constant value after the case.
(2) in a switch statement, you can have any number of case statements, but the constant values after each case cannot be the same.
(3) When executing to the switch statement, the value of the expression is evaluated first, followed by the constant value of the case statement in the following curly braces. When a constant value of the same value as the expression is found, the lookup is no longer continued and is used here as the insertion point for the statement block entering the case statement in the curly braces.
(4) In general, the end of each case statement is the break statement, which is used to jump out of the entire switch statement and continue executing the statement following the switch statement. If you do not use the break statement, continue with the statement block in the following case statement until you encounter a break statement or the entire switch statement ends.
(5) When the constant value in all case statements is not the same as the value of the expression, the statement block in the default statement is executed and nothing is executed if there is no default statement.
② Loop Statements
In Java, three loop statements are supported, namely for statements, while statements, and do-while statements.
The loop control condition can be a constant or variable of a Boolean type, a relational expression, or a logical expression, with the result of a Boolean type.
Format of the Do-while statement:
1 Do 2 { 3 loop body; 4} 5while (expression);
In the Do-while form, there is a semicolon behind the while (expression), and in the while form, no semicolon is required.
Executes the Do-while statement, executes the loop body of the Do-while statement first, and then evaluates the value of the expression. The loop body is executed at least once.
Break statement:
Break marking;
Break statements are usually used in loop statements and switch statements, followed by labels, or not. If there is no label, its function is to cause the program to jump out of the current loop or switch statement, if there is a label, then jump out of the program segment represented by the label. For example, when multiple loops are nested, the loop represented by the label can be popped out through a labeled break statement. In a looping statement, a break statement is typically used with an if statement to jump out of a loop when certain conditions are met.
Continue statement:
Continue marking;
The continue statement is only used in the loop body. Can be followed by the label, or can not follow. If there is no label, its function is to cause the program to end the loop in the loop where the continue statement is located, and start the next loop immediately, and if there is a label, end the loop in the layer of loops marked by the label and start the next loop immediately.
Java Statements and Process control