C中使用switch语句的疑问
我有一个疑问:为什么我在 i=3
和 i=7
时得到输出?
main()
{
int i;
scanf("%d",&i);
switch(i)
{
case 3: printf("message3");
default:
if(i==4)
printf("message4");
case 2:printf("message2");
break;
case 1:printf("Message1");
}
}
I have a doubt: why am I getting outputs when i=3
and when i=7
?
main()
{
int i;
scanf("%d",&i);
switch(i)
{
case 3: printf("message3");
default:
if(i==4)
printf("message4");
case 2:printf("message2");
break;
case 1:printf("Message1");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
好吧,
i == 3 将打印 message3 和 message2
i == 4 将打印 message4 abd message2
i 的每个其他值都会打印 message2
使用
break
终止匹配的处理。Well,
i == 3 will print message3 and message2
i == 4 will print message4 abd message2
every other value of i will print message2
use
break
to terminate processing of a match.@Shubham。如果我重复您已经知道的内容,请原谅我。在某些方面,我正在扩展@Henk 已经指出的内容。
在
switch
语句中,case
和default
标签的作用只是确定从哪里开始执行。一旦确定了第一个标签,其余标签就没有意义了。执行是“失败”。因此,我们必须使用break
来停止并退出switch
。在您的代码中,如果
i == 3
则case 3
是第一行执行。然后执行case 3
、default
和case 2
,然后执行break
。如果
i
是3
以外的任何值,则执行default
,然后执行case 2
,然后退出切换
。我认为由于default
的位置,人们永远不会执行case 1
。@Shubham. Please forgive me if I am repeating what you already know. And in some ways, I am expanding what @Henk already pointed out.
In
switch
statement, the role ofcase
anddefault
labels are only to determine where the execution should start. Once the first label is determined then rest of the labels have no meaning. The execution is "fall through". Therefore, we have to usebreak
to stop and exit theswitch
.In your code, if
i == 3
thencase 3
is the first line of execution. Thencase 3
,default
andcase 2
are executed followed bybreak
.If
i
is any value other than3
thendefault
is executed followed bycase 2
and then exit theswitch
. I don't think one will ever get to executecase 1
due to the location ofdefault
.在情况 3 和
default
之后,您没有break
。You don't have a
break
after case 3 anddefault
.在
case 3
之后没有break
。因此,switch
失败并执行default
语句。After
case 3
there is nobreak
. So,switch
falls through and executesdefault
statement too.default
案例的顺序并不决定该案例何时执行。当 switch 变量与 case 表达式中的任何值都不匹配时,将执行default
case。值
case 1
之后的代码时,1
将打印“Message 1”2
将打印“message 2”当执行case 2
之后的代码时,3
将打印“message 3message2”当执行case 3
之后的代码和fallthrough< /strong> 为default
和 fallthrough 为case 2
4
执行以下代码时将打印“message 4message2”default
和 fallthrough 到case 2
default
case 时,任何其他值都会打印“message 2”,并且fallthrough 到case 2
我有时会首先使用默认值对开关进行编码
The order of the
default
case does not determine when that case is executed. Thedefault
case is executed when the switch variable does not match any of the values in a case expression.For the code above a value of
1
will print "Message 1" when executing the code following thecase 1
2
will print "message 2" when executing the code following thecase 2
3
will print "message 3message2" when executing the code following thecase 3
and fallthrough todefault
and fallthrough tocase 2
4
will print "message 4message2" when executing the code following thedefault
and fallthrough tocase 2
default
case and fallthrough tocase 2
I some times code my switches with the default first