switch 语句 (C)switch Statement (C)

switchcase 语句帮助控制复杂条件和分支运算。The switch and case statements help control complex conditional and branching operations. switch 语句将控制权转交给其主体中的语句。The switch statement transfers control to a statement within its body.

语法Syntax

selection-statement:selection-statement:
switch ( expression ) statementswitch ( expression ) statement

labeled-statement:labeled-statement:
case constant-expression : statementcase constant-expression : statement

default : statementdefault : statement

控制权将传递给其 case constant-expressionswitch ( expression ) 的值匹配的语句。Control passes to the statement whose case constant-expression matches the value of switch ( expression ). switch 语句可以包含任意数量的 case 实例,但同一 switch 语句中的两个 case 常量不能具有相同的值。The switch statement can include any number of case instances, but no two case constants within the same switch statement can have the same value. 语句体的执行从选定语句处开始并继续,直到该主体的结尾或直到 break 语句将控制权传出主体。Execution of the statement body begins at the selected statement and proceeds until the end of the body or until a break statement transfers control out of the body.

switch 语句的使用通常类似于:Use of the switch statement usually looks something like this:

switch ( expression )switch ( expression )

{{

declarationsdeclarations

..

..

..

case constant-expression :case constant-expression :

statements executed if the expression equals thestatements executed if the expression equals the

value of this constant-expressionvalue of this constant-expression

..

..

..

break;break;

default :default :

statements executed if expression does not equalstatements executed if expression does not equal

any case constant-expressionany case constant-expression

}}

可以使用 break 语句结束 switch 语句中特定用例的处理并分支到 switch 语句的末尾。You can use the break statement to end processing of a particular case within the switch statement and to branch to the end of the switch statement. 如果不使用 break,则程序会继续到下一用例,并执行语句,直到达到 break 或该语句的末尾。Without break, the program continues to the next case, executing the statements until a break or the end of the statement is reached. 在某些情况下,可能需要此继续符。In some situations, this continuation may be desirable.

如果任何 case constant-expression 均不等于switch ( expression ) 的值,则会执行 default 语句。The default statement is executed if no case constant-expression is equal to the value of switch ( expression ). 如果省略 default 语句且未找到任何 case 匹配项,则不会执行 switch 主体中的任何语句。If the default statement is omitted, and no case match is found, none of the statements in the switch body are executed. 最多可以有一个 default 语句。There can be at most one default statement. default 语句无需显示在末尾;它可以显示在 switch 语句主体中的任何位置。The default statement need not come at the end; it can appear anywhere in the body of the switch statement. casedefault 标签只能显示在 switch 语句内部。A case or default label can only appear inside a switch statement.

switch expressioncase constant-expression 的类型必须是整型。The type of switch expression and case constant-expression must be integral. 每个 case constant-expression 的值在语句体中必须唯一。The value of each case constant-expression must be unique within the statement body.

switch 语句体的 casedefault 标签只在初始测试中有意义,该测试将确定语句体中开始执行的位置。The case and default labels of the switch statement body are significant only in the initial test that determines where execution starts in the statement body. Switch 语句可以嵌套。Switch statements can be nested. 在执行到任何 switch 语句中之前,将初始化所有静态变量。Any static variables are initialized before executing into any switch statements.

备注

声明可以出现在构成 switch 主体的复合语句的前面,但不执行包含在声明中的初始化。Declarations can appear at the head of the compound statement forming the switch body, but initializations included in the declarations are not performed. switch 语句将控制权直接转交给主体中的一个可执行语句,并绕过包含初始化的行。The switch statement transfers control directly to an executable statement within the body, bypassing the lines that contain initializations.

下面的示例阐释了 switch 语句:The following examples illustrate switch statements:

switch( c )   
{  
    case 'A':  
        capa++;  
    case 'a':  
        lettera++;  
    default :  
        total++;  
}  

如果 c 等于 'A',则会执行此示例中的 switch 主体的所有三个语句,因为不会在以下用例前显示 break 语句。All three statements of the switch body in this example are executed if c is equal to 'A' since a break statement does not appear before the following case. 将执行控制转交给第一个语句 (capa++;) 并继续按顺序转交给主体的其余部分。Execution control is transferred to the first statement (capa++;) and continues in order through the rest of the body. 如果 c 等于 'a',则 letteratotal 将增加。If c is equal to 'a', lettera and total are incremented. 如果 total 不等于 c'A',则仅 'a' 增加。Only total is incremented if c is not equal to 'A' or 'a'.

switch( i )   
{  
    case -1:  
        n++;  
        break;  
    case 0 :  
        z++;  
        break;  
    case 1 :  
        p++;  
        break;  
}  

在此示例中,break 语句跟在 switch 主体的每个语句的后面。In this example, a break statement follows each statement of the switch body. 在执行一个语句后,break 语句将强制从语句体中退出。The break statement forces an exit from the statement body after one statement is executed. 如果 i 等于 -1,则仅 n 将增加。If i is equal to -1, only n is incremented. n++; 语句后面的 break 会导致执行控制传递出语句体,并绕过剩余语句。The break following the statement n++; causes execution control to pass out of the statement body, bypassing the remaining statements. 同样,如果 i 等于 0,则仅 z 将增加;如果 i 等于 1,则仅 p 将增加。Similarly, if i is equal to 0, only z is incremented; if i is equal to 1, only p is incremented. 从严格意义上讲,最后的 break 语句不是必需的,因为控制权将在复合语句的结尾传递出语句体,但将包含它以获得一致性。The final break statement is not strictly necessary, since control passes out of the body at the end of the compound statement, but it is included for consistency.

如下面的示例所示,一个语句可以包含多个 case 标签:A single statement can carry multiple case labels, as the following example shows:

case 'a' :  
case 'b' :  
case 'c' :  
case 'd' :  
case 'e' :  
case 'f' :  hexcvt(c);  

在此示例中,如果 constant-expression 等于 'a''f' 之间的任何字母,则调用 hexcvt 函数。In this example, if constant-expression equals any letter between 'a' and 'f', the hexcvt function is called.

Microsoft 专用Microsoft Specific

Microsoft C 未限制 switch 语句中 case 值的数量。Microsoft C does not limit the number of case values in a switch statement. 该数量仅受可用内存的限制。The number is limited only by the available memory. ANSI C 要求 switch 语句内至少允许使用 257 个 case 标签。ANSI C requires at least 257 case labels be allowed in a switch statement.

Microsoft C 的默认设置是启用 Microsoft 扩展。The default for Microsoft C is that the Microsoft extensions are enabled. 使用 /Za 编译器选项禁用这些扩展。Use the /Za compiler option to disable these extensions.

结束 Microsoft 专用END Microsoft Specific

另请参阅See Also

switch 语句 (C++)switch Statement (C++)