Procedure flow control
Article directory
Summary
There are three process control structures in java: sequence structure, selection structure and cycle structure
Selection structure
if control statement
- Simple if:
Syntax: if (conditional judgment expression){
Execute statement
}
If the return value of the conditional expression is true, the statement inside the if can be executed. Otherwise, if it is false, it cannot be executed. The result of a conditional expression must be true or false
/* If the score of the implementation program is 60 or more, it will pass if(Conditional judgment expression){ Execution statement } If {} is not used as if body, the first line below if is if body * */ public static void main(String[] args) { //Define a variable store score int score = 80; if(score>=60){ System.out.println("pass"); } System.out.println("End of program!"); }
- if...else
Syntax: if (conditional judgment expression){
Execute statement
}else{
Execute statement
}
/* if(Conditional judgment expression){ Execution statement }else{ Execution statement } * */ public static void main(String[] args) { //Define a variable store score int score = 80; if(score>=60){ System.out.println("pass"); }else { System.out.println("Fail,"); } }
- Multiple if... .else if... Else
Syntax: if (conditional judgment expression){
Execute statement
} else if (conditional expression 1){
Execute statement
} else if (conditional expression 2){
......
}else{
}
Note: multiple if when the current if statement is executed when the first expression condition is met, it will not be executed further
/*If the score is greater than or equal to 90 points and less than or equal to 100 points, output level A; otherwise, if it is greater than or equal to 80 points, output level B Otherwise, if it is greater than or equal to 70 minutes, output level C; otherwise, if it is greater than or equal to 60 minutes, output level D, Below 60, output class E */ public static void main(String[] args) { int score = 85; if(score>=90&&score<=100){ System.out.println("A level"); }else if(score>=80){ System.out.println("B level"); }else if(score>=70) { System.out.println("C level"); }else if(score>=60) { System.out.println("D level"); }else { System.out.println("E level"); } //Output: Level B }
- Nested if
The if control statement contains one or more if control statements. Nested if can increase the flexibility of the program through the cooperation of outer statement and inner statement.
Syntax format:
If (expression 1){
if (expression 2){
Sentence
} else{
Sentence
}
}Else if (expression 3){
Sentence
}else{
Sentence
}
/* If today is Saturday or Sunday, go out. If the temperature is above 30 degrees, go swimming, otherwise go climbing. If today is not Saturday or Sunday, work. If the weather is good, talk about business; otherwise, in the company. * */ public static void main(String[] args) { /*Analysis: The outer if determines whether it is Saturday or Sunday, and the inner if determines the weather * */ int day = 6;//Saturday int temp = 32;//The temperature is 32 degrees. String weather = "Good weather";//Good weather if(day == 6 || day == 7){ if(temp>=30){ System.out.println("Swimming"); }else { System.out.println("Mountain climbing"); } }else { if("Good weather".equals(weather)){ System.out.println("Talking about business"); }else { System.out.println("In the company"); } } //Print results: swimming }
switch Statements
Switch: compare the expressions and values in the switch from top to bottom. If the match is successful, execute the statement after the case. When the break is executed, jump out of the switch. If none of the matches are successful, execute the default.
Switch (expression){
case constant 1:
Execute statement 1;
break;
case constant 2:
Execute statement 2;
break;
......
default:
Execute statement;
break;
}
Break can be omitted without error reporting. If omitted, it will penetrate the execution statement (whether it can match or not), and will not jump out until a break is encountered. Therefore, we do not recommend omitting break. Before jdk1.7, swtich expressions could only be integer, character, or enumeration types. But String can be used after jdk1.7.
If it does not match the constant value after any case, the statement in default is executed.
Note: the difference between switch and multiple if: if can be used for equivalent judgment or interval judgment. Switch can only make equivalent judgment, not interval judgment
//1 - > Monday //2--> Tuesday //3 - > Wednesday //4--> Thursday // 5 - > Friday // 6. 7 -- > weekend public static void main(String[] args) { int day = 5; switch(day){ case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; case 3: System.out.println("Wednesday"); break; case 4: System.out.println("Thursday"); break; case 5: System.out.println("Friday"); break; case 6: case 7: System.out.println("Weekend"); break; default: System.out.println("Number is not an integer between 1 and 7!"); } }
Ternary operator
Syntax: logical expression 2? Expression 1: expression 2
? If the result of the previous logical expression operation is true, the result of the entire expression is expression 1? Previous logical expression
If the result of the operation is false, the result of the entire expression is expression 2
Note: the ternary operator is suitable for the judgment of two values and can replace the if else form
public static void main(String[] args) { //1) male; others: Female int gender = 1; // if(gender==1){ // System.out.println('male '); // }else { // System.out.println('female '); // } char res = gender==1?'male':'female'; System.out.println(res); }
Cyclic structure
Cycle: refers to the process of repetition (cycle generally has the precondition of repetition).
Loop classification: while loop, do while loop,
for loop: simplify while syntax, and the execution process is the same as while
Enhanced for loop -- for arrays and collections
- While cycle
Syntax:
While (expression){
Circulatory body
}
If the expression result is true, the loop body is executed, and if it is false, the loop is ended
Judgment before execution
public static void main(String[] args) { //Sum calculation of 1 + 2 + 3 + · + 100 int i = 1; int sum = 0;//Define the sum of variable stores while (i<=100){ sum+=i; i++; } System.out.println(sum); }
When while (true) {}, that is, when the condition is true, a dead loop occurs.
Avoid dead loop / infinite loop: modify the value of loop variable every time.
- do... while cycle
Syntax:
do{
Circulatory body
}While (expression)
Execute first, judge later
Note: do While and while, the former is to execute the body of the loop first, and the latter is to judge the body of the loop first.
public static void main(String[] args) { //Sum calculation of 1 + 2 + 3 + · + 100 int i = 1; int sum = 0;//Define the sum of variable stores do { sum+=i; i++; }while (i<=100); System.out.println(sum);//5050 }
- for cycle
Syntax:
For (expression1; expression2; expression3){
Circulatory body
}
Expression 1 (variable initialization): initialization of the counter, which is initialized only once
Expression 2 (loop condition): judgment of loop condition, multiple execution
Expression 3 (modify value of loop variable): modify counter, execute multiple times
//Sum calculation of 1 + 2 + 3 + · + 100 int sum = 0;//Define the sum of variable stores for (int i =1;i<=100;i++){ sum+=i; } System.out.println(sum);//5050
-
Multiple loop (nested loop)
Loop statements can contain loop statements, and different loop statements can be nested with each other.
public static void main(String[] args) { /*Print 5 rows and 5 columns* ***** ***** ***** ***** ***** */ for (int i=0;i<5;i++){ //Outer print 5 lines System.out.println(); for (int j=0;j<5;j++){ //Print a * without wrapping System.out.print("*"); } } }
-
Using jump statements to control program flow
-
break
The role of break in the loop is to terminate the current loop, and in switch is to terminate the switch.
break is used to jump out of the loop and switch
//Output digit 1-10, if multiple of 4 is encountered, the program will exit automatically for (int i = 1;i<=10;i++){ if(i%4==0){ break; } System.out.println(i);//1 2 3 }
-
continue (can only be used in a loop)
It's also to control the cycle, to give up at a certain time, and then to follow the cycle.
That is to say, the code after this cycle does not execute, and directly enters the position of the cycle condition to start a new cycle.
//The number of the multiple of non-4 in the output number 1-10 for (int i = 1;i<=10;i++){ if(i%4==0){ continue; } System.out.println(i);//1 2 3 5 7 9 10 }
-
return
Ends execution of the current method and exits back to the statement that called the method.
public static void main(String[] args) { //Output numbers 1-10 below 4 for (int i = 1;i<=10;i++){ if(i%4==0){ return; } System.out.println(i); } System.out.println("Loop end"); } //Printing result: 1 2 3 //When i=4, execute the return statement to end the current loop, // It also ends the execution of the entire method, so "loop end" is not printed
-