简体   繁体   English

在 switch 语句中,如果没有给出 break,为什么所有语句都在 true 语句之后执行?

[英]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.

相关问题 如果这三个语句中的任何一个为真,如何获取C退出程序?但是,在处理完所有三个语句之后,该如何处理? - How do I get C to exit the program if any of these three statements are true?But after all three statements have been processed? “ break”仅适用于“ for”,“ while”,“ do-while”,“ switch”和“ if”语句吗? - Does `break` work only for `for`, `while`, `do-while`, `switch' and for `if` statements? 打印所有开关语句 - Printing all switch statements 当我们不在 switch 语句中使用 'break' 语句时,为什么在低于 'default' 的其他情况之前评估 'deafult'? - When we don't use 'break' statement in switch statements,why is 'deafult' evaluated before other cases that is below 'default'? 是否所有if语句都可以转换为等效的switch语句? - Can all if statements be converted to equivalent switch statements? if,else-if和else语句是否在switch case-break语句的“ case”中? - If, else-if, and else statements inside a “case” for a switch-case-break statement? 为什么 switch 语句有奇怪的缩进? - Why the strange indentation on switch statements? 在转换语句中转换IF语句 - Convert IF-statements in Switch-statement 为什么无论输入是什么,都执行两个if语句? - Why both of if statements are executed no matter what the input is? 是否有一种编程语言可以更好地处理 switch 的 break 语句? - Is there a programming language with better approach for switch's break statements?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM