为什么实际上会发生穿透下降。

huangapple go评论66阅读模式
英文:

Why actually the fall through happens

问题

我已经搜索了这个特定的问题很多次,但没有找到任何合适的答案。到处都只是关于当出现"fall through"情况时的解释,他们没有描述我的问题。有谁可以帮帮我吗?

众所周知,如果没有break语句,switch case会进入"fall through"条件。但我想问的是,如果switch case按照case逐个检查值,假设它正在检查第二个case,并且第二个case中没有break,那么根据语言协议,它必须会继续执行下一个case。但既然switch会检查和比较各个case,为什么编译器不会再次检查下一个case呢?我的意思是,为什么它会在不检查case是否相等的情况下运行下一个case的代码块呢?

英文:

I have searched this particular question a lot but didn't find any appropriate answers for it. Everywhere it's only written just only about when fall through happens , they haven't described my issue. Can anyone pls help me .

As we all know that switch case goes to fall through condition if there is no break statement. But i wanna ask that if switch case check value by cases then , if suppose it is checking the second case and there is no break in the second case , then according to language protocols it must falls through but as switch checks and compare the cases then why shouldn't the compiler again checks the next case , i mean then why it runs the block of next case without checking the case equality.

答案1

得分: 0

我认为这个问题非常有趣。

仅在执行流程进入一个 case 时才执行一次等式比较。
不需要使用 break,因为它允许控制执行流程。
在许多 switch/case 的情况下,您可能希望先执行 case B,然后执行所有后续 case(case C,case D 等)。
因此,这个限制是有意设计的。
实际上这并不是一种限制。
它在使用这个语句时提供了一些灵活性。

英文:

I think this question is pretty interesting.

Comparison for equality is done only once, until the execution flow enters a case.
A break is not necessary because it allows to control the execution flow.
There are a lot of situations with switch/case when you'd like to execute case B and after that you'd like to execute all following cases (case C, case D, etc).
So, this limitation is by design.
Actually it's not a limitation.
It gives you some flexibility in using this statement.

huangapple
  • 本文由 发表于 2020年10月11日 22:37:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/64305230.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定