为何以及何时应该goto语句在C / C ++中使用? [英] Why and when should a goto be used in C / C++?
问题描述
我知道很多关于为什么不使用转到
,为什么转到
是不好的做法,为什么<$ C问题$ C>转到是由魔鬼,为什么那些谁键入的手指转到
应扯掉,创造等等......
I know lots of questions about why not use goto
, why goto
is bad practice, why goto
was create by devil, why the fingers of those who type goto
should be ripped off, etc...
而在<一个href=\"http://stackoverflow.com/questions/18507518/what-are-some-better-ways-to-avoid-the-do-while0-hack-in-c\">many回答这个问题,这个问题,甚至在的维基百科有可能找理由PRO /对转到
And in many answers to this questions , this question, and even at wikipedia it's possible to find reasons pro / against goto
.
即使#1有一个转到
标签:)
Even Stackoverflow has a goto
tag :)
但是,很显然,转到
被接受为一个有效的命令,否则你的编译器/间preTER不知道该怎么办。即使在C / C ++有可能使用转到
,而且语言甚至有一个正式的ISO标准。
But, obviously, goto
is accepted as a valid command, otherwise your compiler / interpreter wouldn't know what to do with it. Even in C / C++ it's possible to use goto
, and that language even has a formal, ISO specification.
所以,或许,应该有一些情况下只能通过使用转到
有可能完成一些任务。那么,为何以及何时应该在转到
C中使用/ C ++?
So, probably, there should be some case where only by using goto
it's possible to accomplish some task. Then, why and when should a goto
be used in C / C++ ?
的编辑:的
只是为了确保:
1 - 这个问题并不需要争论。它应该是回答的:给一个或更多的情况下,其中转到
是唯一的解决方案,或说明不,它总是能够避免转到
。当然,我不是唯一的谁可以决定它是否应该被关闭或没有,但我不认为过于宽泛,要求辩论等有理由将其关闭。
1 - this question doesn't require debate. It should be answerable: giving one or more cases where goto
is the only solution, or stating no, it's always possible to avoid a goto
. Of course, I'm not the only who can decide whether it should be closed or not, but I don't think that "too broad, asks for debate, etc" are reasons to close it.
2 - 可能有一些情况下,转到
使code更清洁,更快速,等等。我不希望这些。我想知道,如果存在一种情况,其中转到
是唯一的解决方案(不,它不是说给后藤的好榜样的问题一个DUP)
2 - There might be some cases where goto
make the code cleaner, faster, etc. I don't want those. I want to know if there exists one case where goto
is the only solution (no, it's not a dup of that "give good examples of goto" question)
3 - 我会接受任何downvote,对我脸上的笑容,如果downvote是有道理的。但我pretty确保这个问题有一个答案,并做有关主题previous研究。 Downvoting简单,因为使用了带有G开头的四字母词的禁忌......对不起......
3 - I'll accept any downvote, with a smile on my face, if the downvote is justified. But I'm pretty sure that this question has an answer and has done previous research about the subject. Downvoting simple because those 4-letters taboo word that starts with a "G" was used... sorry...
推荐答案
有没有的情况下,一个转到
是绝对必要的:你可以的总是避免它,如果你真的想,但是这样做纯粹思想体系的原因是错误的。
There is no circumstance under which a goto
is strictly necessary: you can always avoid it if you really want to, but to do that for purely idealogical reasons is a mistake.
例如,我的一个朋友写了一小波变换函数,有(像)15嵌套循环。在这些循环出现错误的情况下,他有一个转到
来清理块只是功能的返回之前
语句。为了达到同样的效果的没有的goto语句,将有涉及设置一个标志,并在每个循环的水平,这将远远低于可读性做了他的code测试。在这种情况下,转到
是正确的选择。
For example, a friend of mine wrote a wavelet transform function that had (something like) 15 nested loops. In the event of an error in those loops, he had a goto
to a cleanup block just before the function's return
statement. To achieve the same effect without a goto would have involved setting a flag and testing it at every loop level, which would have made his code far less readable. In these circumstances, goto
was the right choice.
这篇关于为何以及何时应该goto语句在C / C ++中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!