First, the structure of the program:
In general, the structure of the program contains the following three kinds:
1, the sequence structure 2, the choice Structure 3, the cyclic structure two, the sequence structure
The program is the first and the next line of execution, a statement after execution continue to execute the next statement, until the end of the program
Thirdly, selecting structure selection structure is based on whether the condition is established or not, and then deciding which statements to execute. 3.1, if statement-single branch structure
if (judging condition) {
statement 1;
... ..
Statement 2;
}Example one, comparing the size of 2 integer variables
Packagepb.test; Public classtest4 { Public Static voidMain (string[] args) {intx = 3; inty = 10;//define 2 integer variables, x, ySystem.out.println ("======= begins to compare ======="); if(x>y) {System.out.println ("X is bigger than Y!"); } if(x<y) {System.out.println ("X less than y!"); } if(x==y) {System.out.println ("X equals y!"); } System.out.println ("======= More complete ======="); }}3.2, If...else statement--double branch structure
if (judging condition) {
statement body 1;
}else{
Statement body 2;
}Example two, determine whether a number is odd or even
Packagepb.test; Public classTEST5 { Public Static voidMain (string[] args) {intx=3;//defining medium-to-large variables x if(x%2==0) {//if the remainder is 0, it is even .System.out.println ("X is even"); }Else{//If the remainder is not 0, it is an odd number.System.out.println ("X is odd"); } }}3.3, the trinocular operator when using the three mesh operator, the operand has 3, its format is as follows: variable = condition judgment? Expression 1: Expression 2 depending on whether the condition is set up or not, the expression after the result is ":", or ":", if the condition evaluates to true, then the expression 1 is executed, false executes the expression 2 example three, the maximum value in 2 numbers is calculated
public static void main (string[] args) { //
Define variables to hold the maximum value
int max=0
;
//
Define 2 variables
int x=3
;
int y=10
;
//
use 3 mesh operation to determine if x>y, max=x, otherwise max=y; Max=x>y?
X:Y; System.out.println ( max is: "+
max); }
3.4, If...else if...else Statement-Multi-branch structure if necessary in the IF. else when judging multiple conditions, the if is required. else if ... else statement, in the following format:
if (conditional judgment 1) {
statement body 1;
}else if (conditional judgment 2) {
Statement body 2;
}
...//multiple else if () statements
else{
statement Body 3;
}Example Four,
Packagepb.test; Public classTest6 { Public Static voidMain (string[] args) {intX=3; if(x==1) {System.out.println ("The value of X is 1!"); }Else if(x==2) {System.out.println ("The value of X is 2!"); }Else if(x==3) {System.out.println ("The value of X is 3!"); }Else{System.out.println ("The value of x is not one of them!"); } }}Switch structure to find and execute one of the criteria in a number of selection conditions, in addition to using IF. Else constantly judging, you can also use another more convenient way to multiple-select--switch statements, Syntax format:
switch (expression) {
Case Selection value 1: statement body 1;
Case Selection Value 2: statement body 2;
... ..
Case Select value N: statement body n; break; default: statement body;
}
4.1.Execution process
Example five, judge the student performance, and give the score, 90~100 points, a-level greater than 80 points, B-class, greater than 70 points, output C-class, greater than 60 points, D-class, less than 60 points output e-class
Packagepb.test; Public classTest7 { Public Static voidMain (string[] args) {intScore = 75;//declaring student scores Switch(SCORE/10){ Case10: Case9: System.out.println ("Class A"); Break; Case8: System.out.println ("B-Level"); Break; Case7: System.out.println ("C-Level"); Break; Case6: System.out.println ("Class D"); Break; default: System.out.println ("E-Class"); Break; } }}
Result: Class C
or above if it does not add a break;
Packagepb.test; Public classTest7 { Public Static voidMain (string[] args) {intScore = 75;//declaring student scores Switch(SCORE/10){ Case10: Case9: System.out.println ("Class A"); Case8: System.out.println ("B-Level"); Case7: System.out.println ("C-Level"); Case6: System.out.println ("Class D"); default: System.out.println ("E-Class"); } }}
Results: Class C, Class D, Class E
Java starts from zero (choose structure)