C 开关盒isn';它的执行案例在失败案例中,而不是在失败案例中 #包括 int main() { 开关(2) { 案例1: 如果(1) { 案例2: printf(“hello\n”); }; } 返回0; }
输出=你好 当我在C 开关盒isn';它的执行案例在失败案例中,而不是在失败案例中 #包括 int main() { 开关(2) { 案例1: 如果(1) { 案例2: printf(“hello\n”); }; } 返回0; },c,C,输出=你好 当我在开关中通过2时 case1不正确,然后它也会输入它并在case2中执行代码。 为什么会进入案例1? 谢谢。在开关(2)之后,它将立即跳转到案例2标签。它在案例1中包含的if块中这一事实与此无关案例2:有效的功能与goto标签没有区别,因此无论它在哪里,它都会跳转到它。案例1并非以某种方式被纳入 为澄清起见,适当缩进看起来如下: #include<stdio.h> int main() { switch(2) { case
开关中通过2
时
case1
不正确,然后它也会输入它并在case2
中执行代码。
为什么会进入案例1?
谢谢。在开关(2)
之后,它将立即跳转到案例2
标签。它在案例1
中包含的if
块中这一事实与此无关<代码>案例2:
有效的功能与goto
标签没有区别,因此无论它在哪里,它都会跳转到它。案例1并非以某种方式被纳入
为澄清起见,适当缩进看起来如下:
#include<stdio.h>
int main()
{
switch(2)
{
case 1:
if(1)
{
case 2:
printf("hello\n");
};
}
return 0;
}
#包括
int main(){
开关(2){
案例1:
如果(1){
案例2:
printf(“hello\n”);
}
;
}
返回0;
}
为什么会进入案例1
它不会进入案例1
。事实上,if(1)
在这里没有用处。上述代码相当于
#include<stdio.h>
int main() {
switch(2) {
case 1:
if(1) {
case 2:
printf("hello\n");
}
;
}
return 0;
}
#包括
int main()
{
开关(2)
{
案例1:
案例2:printf(“hello\n”);
}
返回0;
}
要查看
if
的不相关用法,您可以将if(1)
替换为if(0)
,您会发现结果是相同的。FWIW,开关(2)
也没有多大意义。通常打开变量的值。在AICT中,它不会进入案例1:
,而是直接跳到案例2:
。如果(1)也没用的话,我也知道它和你说的很相似,但我的问题是怎么做?
#include<stdio.h>
int main()
{
switch(2)
{
case 1:
case 2: printf("hello\n");
}
return 0;
}