【Preface】
Process control statement:
During the execution of a program, the execution order of each statement has a direct impact on the result of the program. In other words, the process of the program has a direct impact on the operation results. Therefore, we must be clear about the execution process of each statement. Moreover, many times we need to implement the functions we want to complete by controlling the execution order of statements.
Process control statement classification:
Sequential structure selection structure: if statement, switch statement loop structure: while statement, for statement
1. Sequence structure:
It is the simplest and most basic process control in a program, without a specific syntax structure, and it is executed in sequence according to the order of the code. Most of the code in the program is executed in this way.
In general: write in the front and execute first, write in the back and execute later
Sequence structure diagram:
2. Select the structure:
Selection Structure: Also known as branch structure. There are specific syntax rules for the selection structure. The code must perform specific logical operations for judgment. There are two results of the logical operations, so a selection is generated and different codes are executed according to different choices.
Java language provides two choice structure statements:
if statement
Switch statement
3. If statements that select structure:
There are three formats for if statements. Let’s explain in detail below.
1. The first format of if statement: (suitable for a judgment)
Copy the code code as follows: if (relational expression) { statement body;}
Execution process:
First, determine whether the relationship expression is true or false: if it is true, execute the statement body; if it is false, do not execute the statement body
The corresponding flowchart is:
Notes:
A: Relational expressions must be of boolean type, whether simple or complex.
B: If the statement body controlled by the if statement is a sentence, the braces can be omitted; if it is multiple statements, it cannot be omitted. It is recommended not to omit it.
C: Generally speaking: if there is a left brace, there is no semicolon, and if there is a semicolon, there is no left brace.
A: Special case of if(a==b && a==c){…}:
if(a == b){} is written as if(a = b){}
If it is type int, an error will be reported. At this time, it means assigning b to a and leaving a to judge, and a is of type int so an error is reported.
But if a and b are both boolean types, there is no problem.
2. The second format of if statement: (suitable for two judgments)
if(relational expression) { statement body 1;}else { statement body 2;}Execution process:
First, determine whether the relationship expression is true or false: if it is true, execute statement body 1; if it is false, execute statement body 2
The corresponding flowchart is:
We have explained the ternary operator before. After judging based on comparison, the results are given. Therefore, this situation is very similar to the second format of the if statement. In some cases, they should be able to convert each other.
The second format of the if statement and the ternary operator:
All operations of ternary operators can be improved using if statements, otherwise they are not valid.
When does it not work? When the statement body controlled by an if statement is an output statement, it does not work. Because the ternary operator is an operator, a result must be returned; but the output statement cannot be returned as a result.
3. The third format of if statement: (suitable for multiple judgments)
The third format of if statement: if (relational expression 1) { statement body 1; }else if (relational expression 2) { statement body 2; } ... ... else { statement body n+1; }Execution process:
First, determine the relationship expression 1 to see if the result is true or false
If true, execute statement body 1
If it is false, continue to judge the relationship expression 2 to see if the result is true or false
If true, execute statement body 2
If it is false, continue to judge the relationship expression... see if the result is true or false
…
If no relational expression is true, the statement body n+1 is executed.
The corresponding flowchart is:
It should be noted that only one of the statement bodies is executed.
For example: Use the nesting of if statements to get the maximum value of the three data. The code is as follows:
//Implement int max1 with if statement; if(a > b) { max1 = a; }else { max1 = b; } System.out.println("max1:"+max1);3. Switch statement for selecting a structure:
Switch statement format:
switch(expression) { case value 1: statement body 1; break; case value 2: statement body 2; break; ... ... default: statement body n+1; break;}explain:
switch means this is a switch statement
Values of expressions: byte, short, int, char (it can be enumerated after JDK5, and it can be String after JDK7) (This sentence may be an interview question)
The case is followed by the value to be compared with the expression
The statement body part can be one or more statements
break means interrupt, end, and can end switch statement
The default statement indicates that when all the situations do not match, the contents are executed, which is similar to the else of the if statement.
Execution process:
First calculate the value of the expression;
Secondly, compare with case in sequence. Once there is a corresponding value, the corresponding statement will be executed. During the execution process, it will end when encountering break.
Finally, if all cases do not match the value of the expression, the default statement body part will be executed, and the program will be finished.
flow chart:
Notes:
(1) The case can only be a constant, not a variable, and the values after multiple cases cannot appear the same
(2) Can default be omitted?
Can be omitted. Generally not recommended. Unless the judged value is fixed (that is, all possible cases are already listed in the case)
(3) Can break be omitted?
It can be omitted, generally not recommended. Otherwise, the result may not be what you want, and a phenomenon will occur: case penetration.
(4) Do the default position must be at the end?
Can appear anywhere in the switch statement.
(5) End conditions of switch statement:
Situation a: It ends when encountering break, not when encountering default.
Situation b: Execution ends at the end of the program
2. Scenarios used by if and switch:
When making judgments, we have two choices: if statement and switch statement. So, how should we choose which statement to use?
If statement usage scenario:
Judgment about the result being a boolean type
A judgment on a range
Judgement of several constant values
Scenarios for using switch statements:
Judgement of several constant values
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.