The C Programming Language书中3.4 Switch 开头这么写到:
3.4 Switch
The switch statement is a multi-way decision that tests whether an expression matches one of a number of constant integer values, and branches accordingly.
switch (expression) {
case const-expr: statements
case const-expr: statements
default: statements
}
_Each case is labeled by one or more integer-valued constants or constant expressions. _If a case matches the expression value, execution starts at that case. All case expressions must be different. The case labeled default is executed if none of the other cases are satisfied. A default is optional; if it isn't there and if none of the cases match, no action at all takes place. Cases and the default clause can occur in any order.
我不明白,就粗斜体部分,每一个标签应该只是一个整型常量值或者整型表达式。书上为什么写 one or more,意思是每个case标签也可以是多个整型常量值或整型表达式???
你英文不好,注意是labeled,也就是每个常量用标签标记的,所谓标签,就是一个带有冒号的标注。
可以多个case后面放一个表达式。。。
当然可以,如果多个条件都是用同一个规则,那么你就可以把他们放在一起
case 1:
case 2:
case 3:
statements
你英文不好,注意是labeled,也就是每个常量用标签标记的,所谓标签,就是一个带有冒号的标注。
switch(expression)
{
case const-expr:statments;
break;
case const-expr:statments;
case const-expr:statments;
case const-expr:statments;
break;
default: statments;
break;
}
if there is no "break", means more than one "const-expr" will be exec.
switch(expression)
{
case const-expr:statments;
break;
case const-expr:statments;
case const-expr:statments;
case const-expr:statments;
break;
default: statments;
break;
}
if there is no "break", means more than one "const-expr" will be exec.