Switch-case retrun break, switch-caseretrun
{
If statements process two branches, the if-else-if structure must be used when processing multiple branches. However, if there are many branches, the more nested if statement layers, the program is not only huge but also difficult to understand. therefore, the C language provides a condition selection statement specifically used to process multi-branch structures, called a switch statement or a switch statement. use the switch statement to directly process multiple branches (including two branches of course ). the general form is:
Reference
Switch (expression)
{
Case constant expression 1:
Statement 1;
Break;
Case constant expression 2:
Statement 2;
Break;...... Case constant expression n: Statement n; break;
Default: Statement n + 1; break ;}
(1) the execution process of the switch statement is: first calculate the value of the expression in the parentheses after the switch, and then use this value to compare it with the constant expressions of each case in sequence, if the value of the expression in parentheses is equal to the value of the constant expression after a case, execute the statement after this case, and then exit the switch statement when the break statement is executed; if the expression value in parentheses is different from the constant expression after all cases, execute the statement n + 1 after default and exit the switch statement. The program flow turns to the next statement of the switch statement.
(2) if the case contains multiple execution statements, you do not need to increase the brackets like the if statement. After entering a case, all execution statements after this case are automatically executed sequentially. for example:Reference
(3) default is always placed at the end. after default, the break statement is not required. in addition, the default part is not required. If this part is not available, when the value of the expression in the parentheses after the switch is not equal to the value of the constant expression after all the cases, the switch statement is directly exited without executing any branch. in this case, the switch statement is equivalent to an empty statement.
(4) In a switch-case statement, multiple cases can share one execution statement.
......
Case 'A ':
Case 'B ':
Case 'C ':
Method
Break;
......
The constant expression after case actually serves only the statement label, but does not have the function of condition judgment, that is, "Only the Entry Label at the start of execution ". therefore, once it matches the value of the expression in the parentheses behind the switch, it is executed from this label. If the break statement is not encountered after the statement after the case is executed, it will automatically go to the next case to continue execution, instead of judging whether it matches with it. It will not stop execution until it encounters a break statement and exit the break statement. therefore, if you want to execute a switch statement immediately after a case score, you must add a break statement at the end of this branch.
class Test7
public static void main(String[] args) { int i=5; switch(i) { case 1: System.out.println("one"); case 10: System.out.println("ten"); case 5: System.out.println("five"); case 3: System.out.println("three"); default: System.out.println("other"); } }}
The result is:
Five
Three
Other
Switch (expression) {case constant expression 1: Statement 1;... case constant expression 2: Statement 2; default: Statement ;}
The switch is used to determine whether the expression after the case matches the expression after the switch. Once the case matches, the code after the switch is executed sequentially, regardless of whether the subsequent case matches, until the break is met.
In the code given above, because I is equal to 5 and does not match the previous two cases, there is no one or ten in the result. In the third case, 5 matches the I value in the switch, so five is printed. Since break is not encountered, the code is executed in sequence, print three and other
The switch-case statement in Process Control has always been a weakness of mine.
Every time I take an exam or take a test in an interview, the second monk is confused. I think this should be the reason why my foundation is too poor!
To completely solve this heart disease, you have to spend some time!
First, we will discuss the problem from the principle:
Switch (expression) {case constant expression 1: Statement 1;... case constant expression 2: Statement 2; default: Statement ;}
1. default is executed if there is no matching case. default is not required.
2. The statement after case does not need braces. The constant expression constant expressions must be followed by case. The error format is case x.
3. The conditions for the switch statement can be int, byte, char, short, or enum.
4. Once the case matches, the subsequent program code will be executed sequentially, regardless of whether or not the subsequent case matches until the break is met. This feature allows several cases to execute unified statements.
Here are some examples of obfuscation.
1. Standard Type (break statements are available after case)
int i=3; switch(i) { case 1: System.out.println(1); break; case 2: System.out.println(2); break; case 3: System.out.println(3); break; default: System.out.println("default"); break; }
Output result:
3
Return and break:
Int f (int n) {switch (n) {case 1: break; // here is the end switch () case 2: return n; // directly end this function here f }}