Use of if else and switch...case

if:
            1. Judge the specific value
            2. Judge the interval
            3. Judge whether the result of the operation is a boolen type expression true flash

switch:
            1. Judge the specific value
            2. The number of values ​​is fixed.
            For several fixed value judgments, it is recommended to use the switch statement. Because the switch statement loads the specific answers into the memory, the efficiency is relatively higher
———————————————

The if statement can be used for conditional judgment or interval judgment. It ends automatically after executing an if content, and the else can only be executed at the end. Switch can only do fixed value judgment. The value of case is the parameter passed in. When the break ends, the default can be placed in any position.

if(条件1) {
	执行条件1;
} else if(条件2) {
	执行条件2;
} else {
	执行条件3;
}

switch (参数) {
	case 1 :
		执行条件1;
		break;
	case 2 :
		执行条件2;
		break;
	default :
		执行条件3;
}

Performance issues: It is generally recommended to use switch when judging a fixed value, and use if when judging the range. Switch search is similar to binary tree, while if uses linear search, so when the fixed value is judged, it doesn't matter which parameter is used when the parameter condition is within three, but the switch performance is better when the judgment condition is greater than three. .

Guess you like

Origin blog.csdn.net/Sunny_lxm/article/details/101051358