Java Miscellaneous 3 - Conditions of Process Control

Process control

First, let's introduce the flow control of the program.

The so-called flow control is that we have learned variables, data types, operators, expressions in our previous courses, these are the basic elements of computer programming, but the basic execution unit of our program should be a statement, the program execution , the execution is not an expression, but an execution statement. Just like when we were young, we first learned to read words, and then we learned to form words, but eventually we had to learn to make sentences, because writing essays must be written one sentence at a time.

Then the computer program must have multiple statements. The execution order of this statement is called flow control. In my code, who executes so many statements first and who executes later. Each statement is executed several times, whether to execute or not to execute this is called Process control.

Sequential execution process, all our statements are placed in the main function, it must be executed from the first sentence of the main function, then the second sentence, the third sentence, until each sentence in the main function is from top to bottom. When all are executed once, the program execution ends. For example, if I write ten print statements in the main function, they must be executed one by one. This is obviously the most basic execution process of computer software, the sequential execution process.

Conditional branch process

Now what we are going to introduce is the conditional branching process. The conditional branching process is also called the branch structure and the selection structure. It mainly solves the problem that I have to judge a condition before executing some statements, and decide the statement according to this condition, or A block of code that follows executes or does not execute. If the previous sequence process is to execute each sentence once, the conditional branch structure is to execute each sentence 0-1 times. Maybe the condition will be executed, and if the condition is not established, the sentence will not be executed.

if statement

When it comes to the conditional branching process, the most basic syntax is an if statement. The syntax structure is as follows.

~java
if(boolean expression){
block
}
~

The keyword if, what is written in the parentheses behind the If? Must be a boolean expression and cannot write integers . In C language, boolean expressions and integer expressions are the same, 0 means false, non-zero value means true, but in java true is true 1 is 1 true is not 1 1 is not true This is an important difference between java and c , In fact, it is also the embodiment of the simplicity of java.

Explain, for example int a = 10 int b = 5

Originally, I wanted to compare whether a and b are equal, if(a==b), but I made a mistake, and wrote one less equal sign, and wrote it as if(a=b). Then in java an equal sign is the final result of the assignment expression is 5, it will report an error, but in C there is no problem and no error will be reported. Such errors will cause certain problems if they cannot be found in time, and in fact, this is indeed a problem in the development of C. So java made up for this loophole when it was designed. The result is that only boolean expressions can be placed in the parentheses after if in java.

Followed by a code block logic that executes the code block when the boolean expression is true, and does not execute it when it is false. The so-called code block is a curly brace, and we are not uncommon. Classes and methods are all curly braces.

For example, when the user inputs an integer, we use the program to determine whether the integer is even or odd. If it is even, output the even number to the console, and if it is odd, output nothing. code show as below:

~~~java
Scanner s = new Scanner(System.in);
int n = s.nextInt();

if(n%i==0){
System.out.println(“偶数”);
}
~~~

This is the most basic if statement.

if else statement

Then our logic continues to change, if the number is even, print two even words, and if it is odd, print two odd words. Then the second syntax structure is needed at this time, if else.

~java
if(boolean expression){
block1
}else{
block2
}
~

When the Boolean expression is true, the code block 1 is executed, else means otherwise, what is otherwise, that is, when the previous Boolean expression is false, the code block after the else is executed, which is really a conditional branch. , code block 1 and code block 2 will execute one, if the condition is true, code block 1 will be executed, and if the condition is false, code block 2 will be executed.
According to this logic, we make certain modifications to the previous code. If the condition is false, else add a code block to print the odd number.

~~~java
Scanner s = new Scanner(System.in);
int n = s.nextInt();

if(n%i==0){
System.out.println(“偶数”);
}else{
System.out.println(“奇数”);
}
~~~

The above is the if else structure.

if else if

Let's take a look at the multiple if else structure, it will have multiple conditions, the syntax structure is as follows

~java
if(condition 1){
code block 1
}else if(condition 2){
code block 2
}else if(condition 3){
code block 3
}else{
code block 4
}
~

The first is if condition 1, when condition 1 is established, code block 1 will be executed, else if condition 2, else means when condition 1 is not satisfied, enter this else, judge condition 2, and execute condition 2 if the corresponding code block is established, and then execute end. If you enter the third condition, it means that the first two conditions are not satisfied. Then you will judge condition 3. If condition 3 is not satisfied, it will go to the last else. The last one can be added with if or not. In fact, it is If all the previous conditions are not met, it will go to the last else block.

According to this grammatical logic, for example, the input integer is the month, and the season is judged according to the input integer, 1-3 is the Spring Festival, 4-6 is the summer, and so on.

~~~java
Scanner s = new Scanner(System.in);
int n = s.nextInt();

if(n<=3){
System.out.println(“春天”);
}else if(n<=6){
System.out.println(“夏天”);
}else if(n<=9){
System.out.println(“秋天”);
}else{
System.out.println(“冬天”);
}
~~~

The above is about all the grammatical structures of the if statement. One more point. The if statement can be nested according to actual needs. There is no limit to the number of nested layers.

Conditional nature

All java code will eventually be executed by the program as one instruction after another. The CPU has an instruction pointer pointing to the next instruction to be executed. The CPU loads the instruction and executes it according to the instruction of the pointer. Most of the instructions are specific operations and operations. When performing these operations, after an operation is performed, the instruction indicator will automatically point to the next instruction next to it.

And there is a special kind of instruction, called jump instruction, these instructions will modify the value of the instruction indicator, let the CPU jump to a specified place for execution. There are two kinds of jumps, one is conditional jump and the other is unconditional jump. Conditional jump checks a certain condition and jumps if it is satisfied, while unconditional jump directly jumps. When the conditional branch process is executed, if and else are actually converted into these jump instructions.

Notice in the next section: switch selects the structure, why can only types such as byte, int, etc. be filled in the parentheses after switch?


I can't guarantee that every place is right, but I can guarantee that every sentence, every line of code has been scrutinized and considered. I hope that behind every article is my attitude of pursuing a purely technical life.

Always believe that good things are about to happen.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325518940&siteId=291194637