切换没有休息 [英] switch without breaks

查看:106
本文介绍了切换没有休息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,当使用switch语句而没有中断时,即使找到匹配的case,

代码也会继续执行。为什么,

,后续案例未被评估?


我写了一个程序来演示没有断点的开关如何表现

与我预期的行为方式相比。代码包括:

(1)带有休息的switch语句

(2)if / else语句与(1)
$的结果相同b $ b(3)没有中断的switch语句

(4)if / else / goto语句与(3)

(5)的结果相同如果我认为的陈述与(3)


的结果相同,下面是代码,接着是程序的输出,当它是

用-b调用。


#include< stdio.h>


main(int argc,char ** argv )

{

int c;


c = getopt(argc,argv," abcd");

if(c == EOF){

fprintf(stderr,错误:至少有一个选项必须是

used.\ n);

退出(1);

}


printf(" \ n1。使用带休息的开关:\ n" ;);

开关(c){

case''a'':

printf(" option a!\ n" );
休息;

case''b'':

printf(" option b!\ n");

休息;

case''c'':

printf(" option c!\ n");

break ;

case''d'':

printf(" option d!\ n");

break;

默认值:

printf(错误选项!\ n);

}


printf (QUOT; \\\
2。使用if / else:\ n");

if(c ==''a''){

printf(" option a!\ n"如果(c ==''b''){

printf(" option b!\ n");


}否则if(c ==''c''){

printf(" option c!\ n");


}否则if(c ==''d''){

printf(" option d!\ n");

}否则{

printf(错误选项!\ n);

}


printf(" \ n3。使用没有休息的开关:\ n");

开关(c){

case''a'':

printf(" option a!\ n");

case''b'':

printf(" option b!\ n");

case''c'':

printf(" option c!\ n");

case' 'd'':

printf(" option d!\ n");

默认值:

printf("错误选项! \\ n");

}

printf(" \ n4。使用if / else / goto:\ n");

if(c ==''''){

转到a;

}否则if(c ==''b''){

goto b;

}否则if(c ==''c''){

goto c;

} else if(c ==''d''){

goto d;

}


a:printf(" option a!\ n");


b:printf(" option b!\ n");


c:printf(" ;选项c!\ n");


d:printf(" option d!\ n");


printf( 错误选项!\ n;)


printf(" \ n5。使用if:\ n");

if(c ==''a''){

printf(" option a!\ n");

}

if(c ==''b''){

printf(" option b!\ n");

}

if(c ==''c''){

printf(" option c!\ n");

}

if(c ==''d''){

printf(" option d!\ n");

}

printf(错误选项!\ n);

}


=====================

1.使用带休息的开关:

选项b!


2.使用if / else:

选项b!


3.使用不带休息的开关:

选项b!

选项c!

选项d!

错误选项!


4.使用if / else / goto:

选项b!

选项c!

选项d!

错误选项!


5.使用if:

选项b!

错误选项!

I understand that when a switch statement is used without breaks, the
code continues executing even after a matching case is found. Why,
though, are subsequent cases not evaluated?

I wrote a program to demonstrate how a switch without breaks behaves
vs. how I expected it to behave. The code includes:
(1) a switch statement with breaks
(2) the if/else statements that have the same results as (1)
(3) a switch statement without breaks
(4) the if/else/goto statements that have the same results as (3)
(5) the if statements that I thought would have the same results as (3)

Below is the code, followed by the output of the program when it''s
called with "-b".

#include <stdio.h>

main(int argc, char **argv)
{
int c;

c = getopt(argc, argv, "abcd");
if(c == EOF) {
fprintf(stderr, "ERROR: At least one option must be
used.\n");
exit(1);
}

printf("\n1. Using switch with breaks:\n");
switch(c) {
case ''a'':
printf("option a!\n");
break;
case ''b'':
printf("option b!\n");
break;
case ''c'':
printf("option c!\n");
break;
case ''d'':
printf("option d!\n");
break;
default:
printf("wrong option!\n");
}

printf("\n2. Using if/else:\n");
if( c == ''a'' ) {
printf("option a!\n");

} else if( c == ''b'' ) {
printf("option b!\n");

} else if( c == ''c'' ) {
printf("option c!\n");

} else if( c == ''d'' ) {
printf("option d!\n");

} else {
printf("wrong option!\n");
}

printf("\n3. Using switch without breaks:\n");
switch(c) {
case ''a'':
printf("option a!\n");
case ''b'':
printf("option b!\n");
case ''c'':
printf("option c!\n");
case ''d'':
printf("option d!\n");
default:
printf("wrong option!\n");
}
printf("\n4. Using if/else/goto:\n");
if( c == ''a'' ) {
goto a;
} else if( c == ''b'') {
goto b;
} else if( c == ''c'') {
goto c;
} else if( c == ''d'') {
goto d;
}

a: printf("option a!\n");

b: printf("option b!\n");

c: printf("option c!\n");

d: printf("option d!\n");

printf("wrong option!\n");

printf("\n5. Using if:\n");
if( c == ''a'' ) {
printf("option a!\n");
}
if( c == ''b'' ) {
printf("option b!\n");
}
if( c == ''c'' ) {
printf("option c!\n");
}
if( c == ''d'' ) {
printf("option d!\n");
}
printf("wrong option!\n");
}

=====================

1. Using switch with breaks:
option b!

2. Using if/else:
option b!

3. Using switch without breaks:
option b!
option c!
option d!
wrong option!

4. Using if/else/goto:
option b!
option c!
option d!
wrong option!

5. Using if:
option b!
wrong option!

推荐答案



" Evie" < EV **** @ verizon.net>在消息中写道

news:11 ********************* @ g43g2000cwa.googlegro ups.com ...

"Evie" <ev****@verizon.net> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
我理解当使用switch语句而没有中断时,即使找到匹配的case,
代码也会继续执行。为什么,
,后续案例未被评估?
I understand that when a switch statement is used without breaks, the
code continues executing even after a matching case is found. Why,
though, are subsequent cases not evaluated?




因为评估只发生在''switch''术语中,

,它只发生一次。


int main(无效)

{

int x = 2;


开关(x)/ *评价''x''* /

/ *如果x == 1,转到案例1 * /

/ *如果x == 2,转到案例2 * /

/ *任何其他值,转到默认值* /

{

案例1:

案例2:/ *执行将跳转到此处。 * /

/ *''x''将不会再次评估* /


默认值:

}


返回0;

}

-Mike



Because the evaluation only happens in the ''switch'' term,
and it only happens once.

int main(void)
{
int x = 2;

switch(x) /* evaluate ''x'' */
/* if x == 1, goto case 1 */
/* if x == 2, goto case 2 */
/* any other value, goto default */
{
case 1:
case 2: /* execution will jump to here. */
/* ''x'' will not be evaluated again */

default:
}

return 0;
}
-Mike


Evie< ev ****@verizon.net>写道:
Evie <ev****@verizon.net> wrote:
我理解当使用switch语句而没有中断时,即使找到匹配的case,
代码也会继续执行。为什么,
,后续案例未被评估?
(3)一个没有中断的switch语句
(4)if / else / goto语句与(3)
(5)我认为会有的if语句具有相同的结果与(3)相同的结果
I understand that when a switch statement is used without breaks, the
code continues executing even after a matching case is found. Why,
though, are subsequent cases not evaluated? (3) a switch statement without breaks
(4) the if/else/goto statements that have the same results as (3)
(5) the if statements that I thought would have the same results as (3)




这些应该告诉你关于开关你想知道的一切 -

你可以看到,它'本质上是一个美化的goto。这就是为什么4和

不是5重复交换的效果而没有中断。什么将

无论如何都要检查后续案例?


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。



These should tell you everything you wanted to know about switch -
as you can see, it''s essentially a glorified goto. That''s why 4 and
not 5 duplicated the effects of a switch without breaks. What would
be the point of checking subsequent cases anyway?

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


文章< 11 ********************* @ g43g2000cwa.googlegroups。 com>,

Evie< ev **** @ verizon.net>写道:
In article <11*********************@g43g2000cwa.googlegroups. com>,
Evie <ev****@verizon.net> wrote:
我理解当使用switch语句而没有中断时,即使找到匹配的case,
代码也会继续执行。为什么,
,后续案例未被评估?


您的switch语句模型错误。这些案例不是测试

,它们会在遇到时进行检查,它们是标签,表示

从哪里开始处理。一个开关的行为就像你的版本4:

printf(" \ n4。使用if / else / goto:\ n");
if(c ==''a ''){
goto a;
} if if(c ==''b''){
goto b;
} if if(c ==''c ''){
goto c;
}如果(c ==''d''){
转到d;
}
I understand that when a switch statement is used without breaks, the
code continues executing even after a matching case is found. Why,
though, are subsequent cases not evaluated?
Your model of switch statements is wrong. The cases aren''t tests
which are checked as they are encountered, they are labels indicating
where to start processing. A switch behaves just like your version 4:
printf("\n4. Using if/else/goto:\n");
if( c == ''a'' ) {
goto a;
} else if( c == ''b'') {
goto b;
} else if( c == ''c'') {
goto c;
} else if( c == ''d'') {
goto d;
}




所以它产生相同的输出并不奇怪!


- Richard



so it''s not surprising that it produces the same output!

-- Richard


这篇关于切换没有休息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆