使用 if (0) 跳过 switch 中的 case 应该有效吗?
Is using if (0) to skip a case in a switch supposed to work?
我有一种情况,我希望 C++ switch 语句中的两种情况都落入第三种情况。具体来说,第二种情况会落到第三种情况,第一种情况也会落到第三种情况而不经过第二种情况。
我有一个愚蠢的想法,试了一下,成功了!我将第二个案例包装在 if (0) {
... }
中。它看起来像这样:
#ifdef __cplusplus
# include <cstdio>
#else
# include <stdio.h>
#endif
int main(void) {
for (int i = 0; i < 3; i++) {
printf("%d: ", i);
switch (i) {
case 0:
putchar('a');
// @fallthrough@
if (0) { // fall past all of case 1 (!)
case 1:
putchar('b');
// @fallthrough@
}
case 2:
putchar('c');
break;
}
putchar('\n');
}
return 0;
}
当我 运行 它时,我得到了想要的输出:
0: ac
1: bc
2: c
我在 C 和 C++ 中都试过了(都用 clang),结果是一样的。
我的问题是:这个 C/C++ 有效吗?它应该做它做的事吗?
是的,这应该有效。 C 中 switch 语句的 case 标签几乎与 goto 标签完全一样(关于它们如何使用嵌套 switch 语句的一些注意事项)。特别是,它们本身并不为您认为“在案例中”的语句定义块,您可以使用它们跳转到块的中间,就像使用 goto 一样。当跳转到一个块的中间时,与 goto 相同的注意事项适用于跳过变量的初始化等。
话虽如此,在实践中,用 goto 语句编写可能更清楚,如:
switch (i) {
case 0:
putchar('a');
goto case2;
case 1:
putchar('b');
// @fallthrough@
case2:
case 2:
putchar('c');
break;
}
是的,这是允许的,它可以满足您的要求。对于 switch
语句,C++ 标准 says:
case and default labels in themselves do not alter the flow of control, which continues unimpeded across such labels. To exit from a switch, see break.
[Note 1: Usually, the substatement that is the subject of a switch is compound and case and default labels appear on the top-level statements contained within the (compound) substatement, but this is not required. Declarations can appear in the substatement of a switch statement. — end note]
因此,当评估 if
语句时,控制流根据 if
语句的规则进行,而不管中间的 case 标签。
正如其他答案所提到的,这在技术上是标准允许的,但对于代码的未来读者来说,这非常混乱和不清楚。
这就是为什么 switch ... case
语句通常应该用函数调用而不是大量的内联代码来编写。
switch(i) {
case 0:
do_zero_case(); do_general_stuff(); break;
case 1:
do_one_case(); do_general_stuff(); break;
case 2:
do_general_stuff(); break;
default:
do_default_not_zero_not_one_not_general_stuff(); break;
}
我有一种情况,我希望 C++ switch 语句中的两种情况都落入第三种情况。具体来说,第二种情况会落到第三种情况,第一种情况也会落到第三种情况而不经过第二种情况。
我有一个愚蠢的想法,试了一下,成功了!我将第二个案例包装在 if (0) {
... }
中。它看起来像这样:
#ifdef __cplusplus
# include <cstdio>
#else
# include <stdio.h>
#endif
int main(void) {
for (int i = 0; i < 3; i++) {
printf("%d: ", i);
switch (i) {
case 0:
putchar('a');
// @fallthrough@
if (0) { // fall past all of case 1 (!)
case 1:
putchar('b');
// @fallthrough@
}
case 2:
putchar('c');
break;
}
putchar('\n');
}
return 0;
}
当我 运行 它时,我得到了想要的输出:
0: ac
1: bc
2: c
我在 C 和 C++ 中都试过了(都用 clang),结果是一样的。
我的问题是:这个 C/C++ 有效吗?它应该做它做的事吗?
是的,这应该有效。 C 中 switch 语句的 case 标签几乎与 goto 标签完全一样(关于它们如何使用嵌套 switch 语句的一些注意事项)。特别是,它们本身并不为您认为“在案例中”的语句定义块,您可以使用它们跳转到块的中间,就像使用 goto 一样。当跳转到一个块的中间时,与 goto 相同的注意事项适用于跳过变量的初始化等。
话虽如此,在实践中,用 goto 语句编写可能更清楚,如:
switch (i) {
case 0:
putchar('a');
goto case2;
case 1:
putchar('b');
// @fallthrough@
case2:
case 2:
putchar('c');
break;
}
是的,这是允许的,它可以满足您的要求。对于 switch
语句,C++ 标准 says:
case and default labels in themselves do not alter the flow of control, which continues unimpeded across such labels. To exit from a switch, see break.
[Note 1: Usually, the substatement that is the subject of a switch is compound and case and default labels appear on the top-level statements contained within the (compound) substatement, but this is not required. Declarations can appear in the substatement of a switch statement. — end note]
因此,当评估 if
语句时,控制流根据 if
语句的规则进行,而不管中间的 case 标签。
正如其他答案所提到的,这在技术上是标准允许的,但对于代码的未来读者来说,这非常混乱和不清楚。
这就是为什么 switch ... case
语句通常应该用函数调用而不是大量的内联代码来编写。
switch(i) {
case 0:
do_zero_case(); do_general_stuff(); break;
case 1:
do_one_case(); do_general_stuff(); break;
case 2:
do_general_stuff(); break;
default:
do_default_not_zero_not_one_not_general_stuff(); break;
}