Summary of procedure control process

Summary of procedure control process

preface

• process control statement is a statement used to control the execution order of each statement in the program, which can be grouped into groups
Synthesize small logic modules that can complete certain functions.
• its process control mode adopts three basic process structures specified in structured programming, namely:
1. Sequential structure
The program is executed line by line from top to bottom without any judgment and jump.
2. Branch structure
(1) Selectively execute a piece of code according to conditions.
(2) There are two branch statements: if... else and switch case.
3. Circulation structure
(1) Execute a piece of code repeatedly according to the loop conditions.
(2) There are three loop statements: while, do... While and for.
Note: jdk1 5 provides a foreach loop to easily traverse sets and array elements.

1, Sequential structure

Sequential structure

Legal forward references are used when defining member variables in Java. For example:

public class Test{
int num1 = 12;
int num2 = num1 + 2;
}
Error form:
public class Test{
int num2 = num1 + 2;
int num1 = 12;
}

2, Branching structure

1. If else structure

The if statement has three formats:

if (Boolean expression) {
sentence;
}
if (Boolean expression) {
Statement executed when Boolean expression is true;
} else {
Statement executed when Boolean expression is false;
}
if(Conditional expression 1){
Execute code block 1;
}
else if (Conditional expression 2){
Execute code block 2;
}
......
else{
Execute code block n;
}

Branch structure: if else instructions
(1) Conditional expressions must be Boolean expressions (relational or logical expressions), Boolean variables
(2) When there is only one execution statement in the statement block, a pair of {} can be omitted, but it is recommended to keep it
(3) The if else statement structure can be nested as needed
(4) When the if else structure is "one out of many", the last else is optional and can be omitted as needed
(5) When multiple conditions are mutually exclusive, the order between conditional judgment statements and execution statements does not matter
When multiple conditions are "include" relationship, "small upper and large lower / child upper and parent lower"
For example:

public class AgeTest{
	public static void main(String args[]){
	int age = 75;
	if (age< 0) {
	System.out.println("impossible!");
	} else if (age>250) {
	System.out.println("It's a monster!");
	} else {
	System.out.println("People's youth " + age +" ,Ma Ma!");
	}
	}
}

2. Switch case structure

switch(expression){
case Constant 1:
Statement 1;
// break;
case Constant 2:
Statement 2;
// break;
... ...
case constant N:
sentence N;
// break;
default:
sentence;
// break;
} 

For example:

String season = "summer";
switch (season) {
case "spring":
System.out.println("in the warm spring , flowers are coming out with a rush");
break;
case "summer":
System.out.println("Summer heat");
break;
case "autumn":
System.out.println("fresh autumn weather");
break;
case "winter":
System.out.println("Snow in winter");
break;
default:
System.out.println("Season input error");
break;
}

switch statement related rules
(1) The value of the expression in the switch (expression) must be one of the following types: byte, short,
char, int, enumeration (jdk 5.0), String (jdk 7.0);
(2) The value in the case clause must be a constant, not a variable name or an uncertain expression value;
(3) For the same switch statement, the constant values in all case clauses are different from each other;
(4) The break statement is used to make the program jump out of the switch statement block after executing a case branch; as
If there is no break, the program will execute to the end of switch in sequence
(5) The default clause is optional. At the same time, the position is also flexible. When there is no matching case,
Execute default

Summarize the comparison between switch and if statements

(1) If there are few specific values to judge, and they conform to byte, short, char, int, String, enumeration and other types. Although both statements can be used, the swtich statement is recommended. Because the efficiency is slightly higher.
(2) Other situations: for interval judgment and boolean judgment, if is used. If is used in a wider range. In other words, any switch case can be rewritten as if else. Otherwise, it does not hold.

3, Cyclic structure

Loop structure: when certain conditions are met, the function of specific code is executed repeatedly
Classification:
for loop
while loop
Do while loop
The four components of a circular statement
(1) Init_statement
(2) Cycle condition part (test_exp)
(3) Body_statement
(4) Alter_statement

1.for loop

Syntax format

for (①Initialization part; ②Cycle condition section; ④Iterative part){
③Circulating body part;
}

Execution process:
①-②-③-④-②-③-④-②-③-④-...-②

explain:
(1) ② the loop condition part is a boolean expression. When the value is false, exit the loop
(2) ① the initialization part can declare multiple variables, but they must be of the same type, separated by commas
(3) ④ multiple variables can be updated, separated by commas

for loop execution demo

Examples;

public class ForLoop {
	public static void main(String args[]) {
		int result = 0;
		for (int i = 1; i <= 100; i++) {
			result += i;
		}
		System.out.println("result=" + result);
	}
}

2.while loop

Syntax format

①Initialization part
while(②Cycle condition section){
③Circulating body part;
④Iterative part;
}

Execution process:
①-②-③-④-②-③-④-②-③-④-...-②

explain:
(1) Be careful not to forget the declaration part. Otherwise, the cycle will not end and become an dead cycle.
(2) for loops and while loops can be converted to each other
Application examples

public class WhileLoop {
	public static void main(String args[]) {
		int result = 0;
		int i = 1;
		while (i <= 100) {
			result += i;
			i++;
		}
		System.out.println("result=" + result);
	}
}

3. Do while loop

Syntax format

①Initialization part;
do{
③Circulating body part
④Iterative part
}while(②Cycle condition section);

Execution process:
①-③-④-②-③-④-②-③-④-...②

explain:
The do while loop executes the loop body at least once.
Application examples

public class DoWhileLoop {
	public static void main(String args[]) {
		int result = 0, i = 1;
			do {
				result += i;
				i++;
			} while (i <= 100);
		System.out.println("result=" + result);
	}
}

practice:
Read an uncertain number of integers from the keyboard, judge the number of positive and negative numbers, and enter 0 to end the program.

class PositiveNegative {
 public static void main(String[] args) {
 	Scanner scanner = new Scanner(System.in);
 	int positiveNumber = 0;//Count the number of positive numbers
 	int negativeNumber = 0;//Count the number of negative numbers
	for(;;){ //while(true){
	 System.out.println("Please enter an integer:");
		 int z = scanner.nextInt();
			if(z>0){
				 positiveNumber++;
			 }else if(z<0){
 				negativeNumber++;
			 }else
			 break; 
			 }
 		System.out.println("The number of positive numbers is:"+ positiveNumber);
 		System.out.println("The number of negative numbers is:"+ negativeNumber); } }

4. Nested loop

(1) A nested loop is formed when one loop is placed in another loop. Among them,
For, while, do... While can be used as an outer loop or an inner loop.
(2) In essence, nested loops treat the inner loop as the loop body of the outer loop. When there is only inner circulation
When the cycle condition is false, it will completely jump out of the inner cycle and end the current cycle of the outer layer
Start the next cycle.
(3) Let the outer layer cycle times be m times and the inner layer cycle times be n times, then the inner layer cycle body actually needs to execute m*n times.

5. Use of special Keywords: break, continue

Program flow control: the use of break

break statement
The break statement is used to terminate the execution of a statement block

{ ......
break;
......
}

When a break statement appears in a multi-layer nested statement block, a label can indicate which layer of statement block to terminate

```java

label1: { ......
label2: 	{ ......
label3:			 { ......
				break label2;
				......
				}
			}
		}

Examples of break statement usage

public class BreakTest{
	public static void main(String args[]){
		for(int i = 0; i<10; i++){ 
		if(i==3)
			break;
		System.out.println(" i =" + i);
		}
		System.out.println("Game Over!");
	}
}

Program flow control: use of continue

continue statement
(1) continue can only be used in a loop structure
(2) The continue statement is used to skip one execution of its circular statement block and continue the next cycle
(3) When a continue statement appears in the body of a multi-level nested loop statement, a label can indicate which level of loop to skip

Usage example of continue statement:

public class ContinueTest {
	public static void main(String args[]){
		for (int i = 0; i < 100; i++) {
		if (i%10==0)
			continue;
		System.out.println(i);
		}
	}
}

Program flow control: use of return

(1) return: it is not specifically used to end a loop. Its function is to end a method.
When a method executes a return statement, the method will be terminated.
(2) Unlike break and continue, return directly ends the entire method, no matter how many layers of loops the return is in

Description of special process control statement

(1) break can only be used in switch statements and loop statements.
(2) continue can only be used in loop statements.
(3) The functions of the two are similar, but continue is to terminate this cycle, and break is to terminate this layer of cycle.
(4) There can be no other statements after break and continue, because the program will never execute the subsequent statements.
(5) The label statement must immediately follow the head of the loop. Label statements cannot be used before acyclic statements.
(6) Many languages have goto statements. Goto statements can transfer control to any statement in the program at will, and then execute it. But it makes the program error prone. break and continue in Java are different from goto.

Keywords: Java switch

Added by K3nnnn on Fri, 21 Jan 2022 15:35:16 +0200