我怎样才能在没有 goto 的情况下(干净地)重写这个?
我怎样才能干净地做到这一点而不需要 goto?
loop:
if(condition1){
something();
} else if (condition2) {
somethingDifferent();
} else {
mostOfTheWork();
goto loop;
}
我也不想使用休息时间。此外,在执行其他操作之前,预计会循环几次(最多 40 次),因此 mostOfTheWork 部分很可能会尽可能高,即使只是为了可读性。提前致谢。
编辑:这是在编译器优化器在中断时表现不佳的误解下发布的,虽然一开始通常很愚蠢,但我已经证明对自己来说是错误的 通过(性能)实验。另一方面,感谢您的回答;它们是不同风格的有趣读物。
How can I do this cleanly without gotos?
loop:
if(condition1){
something();
} else if (condition2) {
somethingDifferent();
} else {
mostOfTheWork();
goto loop;
}
I'd prefer not to use breaks as well. Furthermore, it is expected to loop several (adv 40) times before doing something else, so the mostOfTheWork part would most likely be as high up as possible, even if just for readability. Thanks in advance.
EDIT: This was posted under the misconception that the compiler optimizer worked poorly with breaks, which, while generally stupid to begin with, I have proven incorrect to myself
through experimentation (of performance). On the other hand, thank you for your answers; they have been interesting reads on varying styles.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
显然,如果任一条件触发,您都会跳出循环。
因此,在两个条件都没有触发时循环,然后看看哪一个条件满足了你的要求。
现在,有人会尖叫说我对条件的评估超出了我的需要
到。为了他们的利益,修改版本:
Clearly, you will break out of the loop if either condition fires.
So, loop while neither condition has fired, then see which one got you.
Now, someone will scream that I evaluated the conditions more than I needed
to. For their benefit, a modified version:
没有休息?
然而,中断是用于流量控制的,它们的作用比 goto 更清楚,所以我建议使用它们。但是在这种情况下,我会推荐@John的答案 作为正确的方法。
Without breaks?
However, breaks are there for flow control, they are much clearer as to what they do than goto, so I would recommend using them. However in this case I would recommend @John's answer as the correct way to do it.
没有 goto 或喙。一如既往的不干净,主观
这当然是非常愚蠢的。只需休息一下即可。
No gotos or beaks. Cleaniless as always, subjective
This is of course, incredibly silly. Just use a break.
计算机科学教授可能已经在你的脑海中灌输了这样的观念:这是不好的,并且会对你的代码造成严重破坏。当正确且有目的地使用时,Goto 实际上非常有效。这就是为什么大多数编程语言都没有放弃它。
在您的特定情况下,goto 很好,因为代码块很小,您可以轻松地看到程序流程。您也可以使用其他控制结构编写意大利面条式代码,尽管这样做有点困难。
You've probably had it drilled into your head by CS professors that it's bad and will wreak havoc in your code. Gotos are actually quite efficient when used properly and purposely. This is why most programming languages haven't dropped it.
In your particular case, the goto is fine because the block of code is small and you can easily see the program flow. You can write spaghetti code with other control structures too, although it's a little harder to do.
由于您没有具体了解某种语言,因此某些语言具有
continue
、next
或skip
,您可以使用它们来代替该转到
。Since you weren't specific about a language, some languages have
continue
,next
orskip
that you can use in place of thatgoto
.在我看来,这个版本的代码最清楚地向后代传达了程序流程,并且最容易扩展。是的,我正在使用
break
。我想不出有什么真正的理由不这样做。如果你确实不想使用
break
,可以使用goto
跳出循环,或者使用return
跳出循环。函数(如果此循环是较大函数的一部分,则您必须重构)。或者
,如果您拒绝使用除 if 和 while 之外的任何流量控制,那么这个怎么样:
另外,请参阅 我对此类问题的规范答案(并投票!)
In my opinion this version of the code most clearly communicates the program flow to posterity, and is the most easily extended. Yes, I'm using
break
. I can't think of any real reason not to.If you really don't want to use
break
, you can usegoto
to get out of the loop, or usereturn
to bail out of the function (if this loop is part of a larger function you'd have to refactor).Or
And if you refuse to use any flow control other than if and while, how about this one:
Also, please see my canonical answer for this sort of question (and vote for it!)