Branch structure (2)

switch case branch structure

switch (expression) {
case constant value 1:
statement body 1;
break;
case constant value 2:
statement body 2;
break;

default:
statement body n + 1;
break;
}

  • Execution flow
    First, the value of the expression is calculated.
    Secondly, it is compared with the case in turn. Once there is a corresponding value, the corresponding statement will be executed. During the execution process, it will end when it encounters a break.
    Finally, if all cases do not match the value of the expression, the body of the default statement will be executed, and then the program will end.
    Code demo
public class DemoSwitch {
	public static void main(String[] args) {
		int num = 10;
		
		switch (num) {
			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; // 最后一个break语句可以省略,但是强烈推荐不要省略
		}
	}
}

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

Case penetrability
In the switch statement, if break is not written after the case, there will be a penetrating phenomenon, that is, the value of the next case will not be judged, and it will run backward directly until it encounters a break, or the overall switch ends.

Code demo

public class Demo08SwitchNotice {
	public static void main(String[] args) {
		int num = 2;
		switch (num) {
			case 1:
				System.out.println("你好");
				break;
			case 2:
				System.out.println("我好");
				// break;
			case 3:
				System.out.println("大家好");
				break;
			default:
				System.out.println("他好,我也好。");
				break;
		} // switch
	}
}

switch-case usage notes

1. In the switch-case structure, there are and can only execute the case, or the statement after default. If there is a statement outside of case and default, it cannot be executed, which is invalid code for Java.
2. The values ​​behind multiple cases cannot be repeated.
3. The parentheses behind the switch can only be the following data types:
basic data types: byte / short / char / int
reference data types: String string, enum enumeration
4. The switch statement format can be very flexible: the order can be reversed, And the break statement can also be omitted.
"Which match case is executed downward from which position until it encounters a break or the end of the whole."
5. In the switch-case structure, default can be omitted, code compilation will not be affected, and there are no syntax errors. But the switch-case comes with an exception handling method.
In the AJGG Alibaba Java development specification, it is not recommended to omit default.

总结
1.switch case结构和 if  else if 结构类似,两者是可以互通的。
2.if else  if结构中可以处理范围性数据,处理的数据区间更广泛
3.switch case处理数据明确性更强,处理针对性选择比较合适。
Published 7 original articles · praised 0 · visits 102

Guess you like

Origin blog.csdn.net/qq_43634768/article/details/105588133