Java learning summary (2021 version) --- Java flow control statement


)

Sequence structure

Branch structure

if branch structure

The first format

if (conditional expression) { executed code block; }

Execution Flow
Insert picture description here
Example

public class IfDemo {
    
    
	public static void main(String[] args) {
    
    
		System.out.println("开始");
		// 定义两个变量
		int a = 10;
		int b = 20;

		if (a == b) {
    
    
			System.out.println("a等于b");
		}
	
		int c = 10;
		if (a == c) {
    
    
			System.out.println("a等于c");
		}
	
		System.out.println("结束");
	}
}

The second format

if (conditional expression) { executed code block; }else{ executed code block; }



Execution Flow
Insert picture description here
Example

public class IfDemo2 {
    
    
	public static void main(String[] args) {
    
    
		System.out.println("开始");
		// 判断给定的数据是奇数还是偶数
		// 定义变量
	int a = 100;
		// 给a重新赋值
	a = 99;

	if (a % 2 == 0) {
    
    
		System.out.println("a是偶数");
	} else {
    
    
		System.out.println("a是奇数");
	}
	
		System.out.println("结束");
	}
}

switch branch structure

Execution process description

First, calculate the value of the expression.
Second, compare it with the case in turn. Once there is a corresponding value, the corresponding statement will be executed. During the execution, it will end when it encounters a break.
Finally, if all the cases do not match the value of the expression, the body of the default statement will be executed, and the program will end.

Execution Flow
Insert picture description here
Example

public class SwitchDemo {
    
    
	public static void main(String[] args) {
    
    
		//创建键盘录入对象
		Scanner sc = new Scanner(System.in);
		
		//接收数据
		System.out.println("请输入一个数字(1-7):");
		int weekday = sc.nextInt();
		
		//switch语句实现选择
		switch(weekday) {
    
    
		case 1:
			System.out.println("星期一");
			break;
		case 2:
			System.out.println("星期二");
			break;
		case 3:
			System.out.println("星期三");
			break;
		case 4:
			System.out.println("星期四");
			break;
		case 5:
			System.out.println("星期五");
			break;
		case 6:
			System.out.println("星期六");
			break;
		case 7:
			System.out.println("星期日");
			break;
		default:
			System.out.println("你输入的数字有误");
			break;
		}
	}
}

Precautions

1. The return value of the expression in switch (expression) must be one of the following types:
  byte, short, char, int, enumeration (jdk1.5), String (jdk1.7)

2. The value in the case clause must be constant, and the values ​​in all case clauses should be different;

3. The default clause is optional. When there is no matching case, default is executed;

4. The break statement is used to make the program jump out of the switch statement block after executing a case branch; if there is no break, the program will be executed sequentially to the end of the switch;

The difference between if branch structure and switch branch structure

  • The if statement is very similar to the switch statement. If the specific value of the judgment is not many, and the four types of byte, short, int, and char are compounded. It is recommended to use the switch statement, because the efficiency is slightly higher;-
  • Other situations: judge the interval, judge whether the result is a boolean type, use if, and the scope of use of if is relatively wide.

Cyclic structure

https://blog.csdn.net/ThinkWon/article/details/101645757

Guess you like

Origin blog.csdn.net/m0_51755061/article/details/113116272