First, the basic usage of switch
The switch statement is a multi-branch selection statement used to implement a multi-branch selection structure. Its general form is as follows:
switch (expression)
{
case constant Expression 1: statement 1
Case constant Expression 2: statement 2
......
case constant Expression N: statement n
Default: Statement n+1
}
which
1) "expression" in parentheses after switch, the result can be an integer value, or it can be a character type of data.
2) When the value of the switch expression is the same as the value of a constant expression in a case statement, the statement in this case is executed, and if none of the values of the switch expression match, then the statement in the default is executed.
3) Each case expression must produce a unique value.
Ii. switch vs. break
Special: Caseonly determines the entrance of the program execution, using break can be reasonable chunking . That is, after the program executes a case clause, the next case clause is executed sequentially. Break allows you to jump out of the switch selection structure after executing a necessary case clause, which solves some problems that cannot be achieved at the same time.
For example:
The results are as follows:
7
8
Default
If each case clause has a break statement, the result will be:7
Note:
1) In general, add a break statement in the last case clause.
2) You also need to add a break statement after default.
3) If there are more than one statement in the case clause, the {} is applied.
4) Multiple case can share one execution statement.
This article is from the "unintentional persistent" blog, please be sure to keep this source http://10740590.blog.51cto.com/10730590/1700639
Basic usage and break of switch