Problem:
Break plays a role in the loop flow control of the for loop, while loop, and stops execution of the statement after the break, jumps out of the loop, and jumps out of the loop control body;
In the switch condition selection, there is no circular control, what is the role of the break?
Solution:
1. The execution flow of the switch statement is:
The value of the expression in the parentheses after the switch is evaluated first, and then the constant expression of each case is compared with this value.
If the value of the expression in the parentheses is equal to the value of the constant expression after the case, execute the statement following the case, execute the break statement and exit the switch statement, and the program flow turns to the next statement of the switch statement;
If the value of an expression in parentheses is unequal to the constant expression after all the case, execute the statement following the default, and then exit the switch statement, and the program flow turns to the next statement of the switch statement.
In a switch-case statement, multiple case can share a single execution statement, such as:
case constant Expression 1:
Case constant Expression 2:
Case constant Expression 3:
Statement
Break
The effect of case statements can be seen:
The constant expression at the end of the case actually only acts as a statement label, but does not have a condition to judge the function, that is, "just start the entrance marking of the execution department".
Therefore, once the value of the expression in the parentheses behind the switch matches, the execution begins at this label;
And after executing a statement after a case, if you do not encounter a break statement, you automatically go to the next cases to continue execution, and no longer determine whether to match, until the break statement to stop execution, exit the switch statement.
Therefore, if you want to jump out of the switch statement immediately after executing a single case, you must add a break statement at the end of this branch.
2. As you can see from the above description, the role of the break in the switch conditional selection statement is essentially the same as in the circular control statement:
Are statements that do not execute after a break, and exit the switch statement;
3. Unlike the continue statements in the loop control, there is no continue statement in the switch conditional selection statement;
Ok problem solved.