switch statement
开关语句
根据条件的值,将控制转移到多个语句之一。
句法
attr(optional) switch ( condition ) statement | attr(optional) switch ( condition ) statement | | | (until C++17) |
---|---|---|---|---|
attr(optional) switch ( condition ) statement | | | ||
attr(optional) switch ( init-statement(optional) condition ) statement | attr(optional) switch ( init-statement(optional) condition ) statement | | | (since C++17) |
attr(optional) switch ( init-statement(optional) condition ) statement | | |
attr(C++11) | - | any number of attributes |
---|---|---|
condition | - | any expression of integral or enumeration type, or of a class type contextually implicitly convertible to an integral or enumeration type, or a declaration of a single non-array variable of such type with a brace-or-equals initializer. |
init-statement(C++17) | - | either an expression statement (which may be a null statement ";") a simple declaration, typically a declaration of a variable with initializer, but it may declare arbitrary many variables or structured bindings Note that any init-statement must end with a semicolon ;, which is why it is often described informally as an expression or a declaration followed by a semicolon. |
statement | - | any statement (typically a compound statement). case: and default: labels are permitted in statement and break; statement has special meaning. |
- 安表达式语句%28,这可能是
空语句
“;
“%29
- 阿简单声明,通常是带有初始化项的变量声明,但它可以声明任意多个变量或结构化绑定。
注意,任何init语句都必须以分号结尾。;
,这就是为什么它经常被非正式地描述为一个表达式或一个声明,后面跟着分号。声明-任何陈述%28通常是复合语句%29。case:
和default:
在声明中允许使用标签break;
语句具有特殊的含义。
attr(optional) case constant_expression : statement | (1) | |
---|---|---|
attr(optional) default : statement | (2) | |
constant_expression | - | a constant expression of the same type as the type of condition after conversions and integral promotions |
---|
解释
开关语句的主体可能有任意数目的case:
标签,只要所有常量的值[医]转换/升级后的表达式是唯一的%28%29。最多一次default:
标签可能存在%28,尽管嵌套的开关语句可能使用它们自己的default:
标签或有case:
其常量与封闭开关%29中使用的相同的标签。
如果条件计算为等于常量值的值。[医]表达式,然后将控件转移到标记为该常量的语句。[医]表情。
如果条件计算为不匹配%27T的值,则为case:
标签和default:
标签存在,则控件被转移到标记为default:
标签。
大破断语句中遇到的开关语句退出:
二次
switch(1) {
case 1 : cout << '1'; // prints "1",
case 2 : cout << '2'; // then prints "2"
}
二次
二次
switch(1) {
case 1 : cout << '1'; // prints "1"
break; // and exits the switch
case 2 : cout << '2';
break;
}
二次
Compilers may issue warnings on fallthrough (reaching the next case label without a break) unless the attribute [fallthrough] appears immediately before the case label to indicate that the fallthrough is intentional. If init-statement is used, the switch statement is equivalent to. { init_statement switch ( condition ) statement } Except that names declared by the init-statement (if init-statement is a declaration) and names declared by condition (if condition is a declaration) are in the same scope, which is also the scope of statement. | { init_statement switch ( condition ) statement } | | | (since C++17) |
---|---|---|---|---|
{ init_statement switch ( condition ) statement } | | |
因为控制权的转移不允许进入范围对于变量,如果在语句中遇到声明语句,则必须在其自己的复合语句中限定其作用域:
二次
switch(1) {
case 1: int x = 0; // initialization
std::cout << x << '\n';
break;
default: // compilation error: jump to default: would enter the scope of 'x'
// without initializing it
std::cout << "default\n";
break;
}
二次
二次
switch(1) {
case 1: { int x = 0;
std::cout << x << '\n';
break;
} // scope of 'x' ends here
default: std::cout << "default\n"; // no error
break;
}
二次
关键词
switch
,,,case
,,,default
...
例
下面的代码显示了开关
声明。
二次
#include <iostream>
int main()
{
int i = 2;
switch (i) {
case 1: std::cout << "1";
case 2: std::cout << "2"; //execution starts at this case label
case 3: std::cout << "3";
case 4:
case 5: std::cout << "45";
break; //execution of subsequent statements is terminated
case 6: std::cout << "6";
}
std::cout << '\n';
switch (i) {
case 4: std::cout << "a";
default: std::cout << "d"; //there are no applicable constant_expressions
//therefore default is executed
}
std::cout << '\n';
switch (i) {
case 4: std::cout << "a"; //nothing is executed
}
// when enumerations are used in a switch statement, many compilers
// issue warnings if one of the enumerators is not handled
enum color {RED, GREEN, BLUE};
switch(RED) {
case RED: std::cout << "red\n"; break;
case GREEN: std::cout << "green\n"; break;
case BLUE: std::cout << "blue\n"; break;
}
// pathological examples
// the statement doesn't have to be a compound statement
switch(0)
std::cout << "this does nothing\n";
// labels don't require a compound statement either
switch(int n = 1)
case 0:
case 1: std::cout << n << '\n';
// Duff's Device: http://en.wikipedia.org/wiki/Duff's_device
}
二次
产出:
二次
2345
d
red
1
二次
另见
C开关文件
*。
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。