Process control statement

Process control statement

Process control

summary

In the process of a program execution, 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 running results. Therefore, we must know the execution process of each statement. Moreover, many times we need to control the execution order of statements to realize the functions we want to complete.

Sequential structure

public static void main(String[] args){
	//Execute sequentially, and run from top to bottom according to the writing order
	System.out.println(1);
	System.out.println(2);
	System.out.println(3);
}

Judgment statement

Judgement 1: if

1. The first format of if statement: if

if(Relational expression){
	Statement body;
}

2. Execution process

First, judge the relational expression to see if the result is true still false
 If it is true Execute the body of the statement
 If it is false The body of the statement is not executed

public static void main(String[] args){
	System.out.println("start");
	// Define two variables
	int a = 10;
	int b = 20;
	//Variables are judged using if
	if (a == b){
		System.out.println("a be equal to b");
	}
	int c = 10;
	if(a == c){
		System.out.println("a be equal to c");
	}
	System.out.println("end");
}

Judgment statement 2: if... else

1. The second format of if statement: if... else

if(Relational expression) {
	Statement body 1;
}else {
	Statement body 2;
}

2. Execution process

First, judge the relational expression to see if the result is true still false
 If it is true Execute statement body 1
 If it is false Execute statement body 2

public static void main(String[] args){
	// Determine whether the given data is odd or even
	// Define variables
	int a = 1;
	if(a % 2 == 0) {
		System.out.println("a It's an even number");
	} else{
		System.out.println("a It's an odd number");
	}
	System.out.println("end");
}

Judgment statement 3:if... else if... else

1. The third format of if statement: if... else if... else

if (Judgment condition 1) {
	Execute statement 1;
} else if (Judgment condition 2) {
	Execute statement 2;
}
...
}else if (Judgment conditions n) {
	Execute statement n;
} else {
	Execute statement n+1;
}

2. Execution process

First, judge the relationship expression 1 to see if the result is true still false
 If it is true Execute statement body 1
 If it is false Continue to judge relational expression 2 and see if the result is true still false
 If it is true Execute statement body 2
 If it is false Just continue to judge the relational expression... And see what the result is true still false
...
If there is no relationship, the expression is true,Execute the body of the statement n+1. 

public static void main(String[] args) {
	// The relationship between x and y is as follows:
	// x>=3 y = 2x + 1;
	//‐1<=x<3 y = 2x;
	// x<=‐1 y = 2x – 1;
	// According to the given value of x, the value of y is calculated and output.
	// Define variables
	int x = 5;
	int y;
	if (x>= 3) {
		y = 2 * x + 1;
	} else if (x >= ‐1 && x < 3) {
		y = 2 * x;
	} else {
		y = 2 * x ‐ 1;
	}
	System.out.println("y The values are:"+y);
}

Interchange of if statement and ternary operator

In some simple applications, if statements can be used interchangeably with ternary operators.

public static void main(String[] args) {
	int a = 10;
	int b = 20;
	//Define variables to hold the larger values of a and b
	int c;
	if(a > b) {
		c = a;
	} else {
		c = b;
	}
	//The above functions can be rewritten as ternary operators
	c = a > b ? a:b;
}

Select statement

Select statement: switch

1.switch statement format:

switch(expression) {
	case Constant value 1:
		Statement body 1;
		break;
	case Constant value 2:
		Statement body 2;
		break;
	...
	default:
		Statement body n+1;
		break;
}

2. Execution process

First, calculate the value of the expression
 Secondly, and case Compare in turn. Once there is a corresponding value, the corresponding statement will be executed. In the process of execution, the break It will end.
Finally, if all case If they do not match the value of the expression, they will be executed default The body of the statement, and then the program ends.

public static void main(String[] args) {
	//Define the variable and judge the day of the week
	int weekday = 6;
	//switch statement implementation selection
	switch(weekday) {
		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:
			System.out.println("Saturday");
			break;
		case 7:
			System.out.println("Sunday");
			break;
		default:
			System.out.println("The number you entered is wrong");
			break;
	}
}

In the switch statement, the data type of the expression can be byte, short, int, char, enum (enumeration). String can be received after JDK7.

case penetration

In the switch statement, if break is not written after the case, penetration will occur, that is, it will not judge the value of the next case and run directly backward until a break is encountered or the overall switch ends.

public static void main(String[] args) {
	int i = 5;
	switch (i){
	case 0:
		System.out.println("implement case0");
		break;
	case 5:
		System.out.println("implement case5");
	case 10:
		System.out.println("implement case10");
	default:
		System.out.println("implement default");
}
}

In the above program, after executing case5, because there is no break statement, the program will go back all the time. It will not judge the case or ignore the break, and directly run the complete switch.
Because of the penetration of case, beginners must write break when writing switch statements.

Circular statement

Cycle overview

A loop statement can repeatedly execute a piece of code when the loop conditions are met. This repeatedly executed code is called a loop body statement. When the loop body is repeatedly executed, the loop judgment condition needs to be modified to false at an appropriate time to end the loop, otherwise the loop will be executed all the time and form a dead loop.

Loop statement 1:for

1.for loop statement format:

for(Initialization expression①; Boolean expression②; Step expression④){
	Circulatory body③
}

2. Execution process

Execution sequence:①②③④>②③④>②③④...②Not satisfied.
①Responsible for completing the initialization of loop variables
②It is responsible for judging whether the cycle conditions are met. If not, it will jump out of the cycle
③Executed statement
④After the cycle, the change of variables involved in the cycle conditions

public static void main(String[] args) {
	//The console outputs HelloWorld 10 times without using a loop
	System.out.println("HelloWorld");
	System.out.println("HelloWorld");
	System.out.println("HelloWorld");
	System.out.println("HelloWorld");
	System.out.println("HelloWorld");
	System.out.println("HelloWorld");
	System.out.println("HelloWorld");
	System.out.println("HelloWorld");
	System.out.println("HelloWorld");
	System.out.println("HelloWorld");
	System.out.println("‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐");
	//Improve with cycle, cycle 10 times
	//The defined variable starts from 0 and the loop condition is < 10
	for(int x = 0; x < 10; x++) {
		System.out.println("HelloWorld"+x);
	}
}

Loop statement 2: while

1.while loop statement format:

Initialization expression①
	while(Boolean expression②){
		Circulatory body③
		Step expression④
}

2. Execution process

Execution sequence:①②③④>②③④>②③④...②Not satisfied.
①Responsible for completing the initialization of loop variables.
②It is responsible for judging whether the cycle conditions are met. If not, it will jump out of the cycle.
③Specific executed statements.
④After the cycle, the change of the cycle variable.

Loop statement 3: do... while

1.do... while loop format

Initialization expression①
	do{
	Circulatory body③
	Step expression④
}while(Boolean expression②);

2. Execution process

Execution sequence:①③④>②③④>②③④...②Not satisfied.
①Responsible for completing the initialization of loop variables.
②It is responsible for judging whether the cycle conditions are met. If not, it will jump out of the cycle.
③Executed statement
④Changes of cyclic variables after cycling


Characteristics of do... while loop: unconditionally execute the loop body once. Even if we directly write the loop condition as false, it will still loop once. Such a loop has certain risks, so beginners are not recommended to use the do... while loop.

Differences between circular statements

Minor differences between for and while:
The variable controlled by the control condition statement can no longer be accessed after the end of the for loop, and the while loop can continue to be used. If you want to continue to use it, use while, otherwise it is recommended to use for. The reason is that when the for loop ends, the variable disappears from memory, which can improve the efficiency of memory use.
for is recommended when the number of cycles is known, and while is recommended when the number of cycles is unknown.

jump statement

break

Usage scenarios: termination switch Or cycle
	Select structure in switch Statement
	In a loop statement
	It is meaningless to leave the existence of the usage scenario

continue

Usage scenario: end this cycle and continue the next cycle

Expand knowledge points

Dead cycle

Dead loop: that is, the condition in the loop is always true,A dead cycle is a cycle that never ends. For example: while(true){}. 

Nested loop

The so-called nested loop means that the loop body of one loop is another loop. For example, there is also a for loop in the for loop, which is a nested loop. Total number of cycles = number of outer cycles * number of inner cycles

1. Nested loop format:

for(Initialization expression①; Cycle condition②; Step expression⑦) {
	for(Initialization expression③; Cycle condition④; Step expression⑥) {
		Execute statement⑤;
	}
}

2. Nested loop execution process:

Execution sequence:①②③④⑤⑥>④⑤⑥>⑦②③④⑤⑥>④⑤⑥
External circulation once, internal circulation many times.

Tags: Java Back-end

Posted on Mon, 22 Nov 2021 11:57:50 -0500 by shai1