Home>Article>Backend Development> In the switch statement, what can be the label after the case?
In the switch statement, the label after case can only be a constant expression. In a specific switch statement, each case constant must have a unique value and cannot be repeated. But the switch statement can contain multiple case labels.
# The switch statement is used to perform different actions based on different conditions.
Grammar format:
switch (变量表达式) { case 常量1: 语句;break; case 常量2: 语句;break; case 常量3: 语句;break; ... case 常量n: 语句;break; default: 语句;break; }
The switch statement is a conditional selection statement. Find the same case value as the program entry after execution; if all cases are not satisfied , then look for the default entry; if the default entry is not found, exit the entire switch statement.
The constant after case refers to a constant expression. Within a particular switch statement, each case constant must have a unique value and cannot be repeated. But a switch statement can contain multiple case labels.
The default label is optional and can be placed anywhere in the switch statement body. If there is no default label and no case constants match the evaluation of the switch statement control expression, then none of the statements in the switch statement body will be executed. In this case, program flow switches to executing the statements following the switch statement body.
The body of a switch statement is usually a block of statements starting from a case label. If there are statements before the first case label, these statements will not be executed.
C language labels are only used to identify destinations to which the program flow may jump. The tags themselves have no effect on the program. Therefore, after jumping from the switch to the first qualified case label, the program will continue to execute sequentially without being affected by other labels.
If after this qualified case label, statements in other case labels should be ignored, a break statement should be added after the last statement executed so that the program flow jumps directly to switch after the statement body.
If variables need to be declared within a switch statement, then these variables should be declared in a nested inner statement block, as follows:
switch ( x ) { case C1: { int temp = 10; // 声明temp,仅为该case使用 /* ... */ } break; case C2: /* ... */ }
In the switch expression, integers are applied promote. The case constant is converted to a type that matches the result of the switch expression evaluation.
You can also use the else if statement to achieve the switch/case statement effect. But if you judge the program flow based on the value of an integer expression, you should use a switch/case statement, which can improve the readability of the code.
Recommended: "c Language Tutorial"
The above is the detailed content of In the switch statement, what can be the label after the case?. For more information, please follow other related articles on the PHP Chinese website!