12--Program structure (program execution flow)--Select structure

Generally speaking, there are three types of program structure: sequence structure, selection structure and loop structure. These three different structures have a common feature. They have only one entrance and only one exit. The single entry and exit makes the program easy to read, easy to maintain, and easy to debug.

  1. Sequence structure: The program is executed one by one from top to bottom. After one statement is executed, the next statement continues to be executed until the end of the program, as shown in the figure:
  2. Selection structure: The selection structure is to decide which statements to execute according to whether the conditions are true or not. A structure: as shown in the figure:
  3. Loop structure: The loop structure determines the number of executions of the program section according to whether the judgment condition is established or not. This program section is called the loop body, as shown in the figure:
    Insert picture description here

Choose a structure

The selection structure includes if, if...else and switch statements. After the program is added with the selection structure, it is like adding a "crossroad". According to different choices, the program will run with different results.
If statement
if statement syntax:
if (judgment condition) { statement 1; ... } If there is only one statement to be processed in the body of the if statement, the left and right braces can be omitted. When the value of the judgment condition is satisfied (true), the statements in the braces will be executed one by one. The flow control chart of if : The execution steps of if statement: (1) Judge the result of expression (2) If the result of expression is true, execute the statement (3) If the result of expression is false, then skip the statement Example 1:




Insert picture description here




package com.qwy;

public class Demo01 {
    
    
	public static void main(String[] args) {
    
    
		int x=3;
		int y=4;
		System.out.println("*********开始比较**********");
		if(x>y){
    
    
			System.out.println("x比y大");
		}
		if(x<y){
    
    
			System.out.println("x比y小");
		}
		System.out.println("*********结束比较**********");
	}
}

Execution result:
start comparison *
x is smaller than y
end comparison *
if…else… statement
Syntax structure:
if (judgment condition) { statement body 1; }else{ statement body 2; }



When there is a branch judgment statement in the program, you can use if...else statement for processing. When the judgment condition is established, the main body of the if statement is executed. When the judgment condition is not established, the main body of the statement after the else is executed, and the flow control chart of if...else Show: Insert picture description here
Example 2:

package com.qwy;

public class Demo02 {
    
    
	public static void main(String[] args) {
    
    
		int num = 3;
		if (num % 2 == 1) {
    
    
			System.out.println("nums是奇数");
		} else {
    
    
			System.out.println("num是偶数");
		}
	}
}

Operation result:
nums is an odd
ternary operator (ternary operator).
There is an operator that can be equivalent to a statement that uses if...else to assign a variable, that is, the ternary operator: its format: variable=judgment condition? Expression 1: Expression 2.
Example 3:

package com.qwy;

public class Demo03 {
    
    
	public static void main(String[] args) {
    
    
		int max = 0;
		int x = 2;
		int y = 4;
		max = x > y ? x : y;
		System.out.println(max);
	}
}

Running results:
4
From the running results, the use of the ternary operator is more concise. It can replace a long string of if...else with one statement.
if...else if...else statement
If you need to judge multiple conditions in if...else, use if...else if...else statement, its syntax:
if(condition judgment 1) { statement body 1; }else if(condition judgment 2 ){ Statement body 2; } …//Multiple else if() statements else{statement body 3} if…else if flow control diagram: Example 4:







Insert picture description here

package com.qwy;

public class Demo04 {
    
    
	public static void main(String[] args) {
    
    
		int x = 5;
		if (x == 1) {
    
    
			System.out.println("x的值是1");
		} else if (x == 2) {
    
    
			System.out.println("x的值是2");
		} else if (x == 5) {
    
    
			System.out.println("x的值是5");
		} else {
    
    
			System.out.println("x的值不在1,2,5中");
		}
	}
}

Operation result:
The value of x is 5. When the
switch statement
finds and executes one of the statements that meets the judgment condition among many conditions, in addition to using if...else continuous judgment, you can also use a more convenient way, that is, multiple choices—— switch statement. Its grammatical format:
switch(expression){ case selection value 1: statement body 1; break; case selection value 2: statement body 2; break; …… case selection value n: statement body n; break; default: statement body; } Switch flow control diagram: The execution steps of switch flow control: (1) The switch statement first calculates the expression in the brackets, and the result is numbers, characters, enumerations (after JDK1.5), and strings (after JDK1.7) ( Two) According to the value of the expression, check whether it matches the selected value behind the case. If the selected value of the case does not match, the statement contained in the default is executed, and the switch statement is left after the execution is completed. (3) If the selected value of a case matches the result of the expression, the statement contained in the case will be executed, and the switch statement will not be left until after the break statement. (4) If the break statement is not added after the case statement, the switch statement will be executed until the end of the switch statement. (5) If the default execution statement is not defined, nothing is executed and the switch statement is directly exited. Example 5:










Insert picture description here






package com.qwy;

public class Demo05 {
    
    
	public static void main(String[] args) {
    
    
		int x = 3;
		int y = 4;
		char oper = '-';
		switch (oper) {
    
    
		case '+': {
    
    
			System.out.println("x+y=" + (x + y));
			break;
		}
		case '-': {
    
    
			System.out.println("x-y=" + (x - y));
			break;
		}
		case '*': {
    
    
			System.out.println("x*y=" + (x * y));
			break;
		}
		case '/': {
    
    
			System.out.println("x+y=" + (x / y));
			break;
		}
		default: {
    
    
			System.out.println("未知的操作");
			break;
		}
		}
	}
}

Operation result:
xy=-1
Example 6: Verify the role of break

package com.qwy;

public class Demo06 {
    
    
	public static void main(String[] args) {
    
    
		int x=3;
		int y=4;
		char oper='-';
		switch (oper) {
    
    
			case '+':{
    
    
				System.out.println("x+y="+(x+y));
				//break;
			}
			case '-':{
    
    
				System.out.println("x-y="+(x-y));
				//break;
			}
			case '*':{
    
    
				System.out.println("x*y="+(x*y));
				//break;
			}
			case '/':{
    
    
				System.out.println("x+y="+(x/y));
				//break;
			}
			default:{
    
    
				System.out.println("未知的操作");
				//break;
			}		
		}
	}
}

Operation result:
xy=-1
x*y=12
x+y=0
unknown operation

It can be seen from the running results that after the first condition of the program is met, because there is no corresponding break statement set, the program is executed from the position where the first condition is met, and the switch will not exit until the last condition is executed.

Guess you like

Origin blog.csdn.net/qwy715229258163/article/details/113855385