[英]In switch statements, why do all statements get executed after a true statement if break is not given?
In a switch
/ case
statement, the case
statements is for checking the condition, so why should it execute the statements which are false?在
switch
/ case
语句中, case
语句用于检查条件,那么它为什么要执行错误的语句呢? Even if it continues execution of all the cases up to the end of the program if break
is not given, won't the case
statement check the condition.如果没有给出
break
,即使它继续执行所有case直到程序结束, case
语句也不会检查条件。 Isn't its internal working same as if
/ else
statements?它的内部工作方式与
if
/ else
语句不同吗? If not, then how does it work internally?如果不是,那么它在内部如何工作? Language: C
语言:C
No, a case label (which isn't a statement) doesn't check any condition.不,一个案例 label (这不是一个语句)不检查任何条件。 In fact it doesn't do anything at all (notice how you can have multiple cases in a row?) and doesn't produce any code.
事实上,它根本没有做任何事情(注意你如何可以连续处理多个案例?)并且不产生任何代码。 Upon entering the switch, execution jumps to the matching case.
进入开关后,执行跳转到匹配的案例。 And that's it.
就是这样。 Once you're in the block, the labels don't do anything, but a
break
will get you out.一旦你进入街区,标签不会做任何事情,但
break
会让你出去。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.