问题描述
伙计们,
需要一些常见的观点
我们可以选择使用goto在C语言中,但是大多数测试书
(甚至K& R)建议不要使用它。
我的个人经验是goto有时会使程序更多
更干净,易于理解,也非常有用(错误处理
案例)。
为什么goto被文明c程序员社区禁止。
是否有任何技术效率低下。
谢谢
Guys,
Need some of your opinion on an oft beaten track
We have an option of using "goto" in C language, but most testbooks
(even K&R) advice against use of it.
My personal experience was that goto sometimes makes program some more
cleaner and easy to understand and also quite useful (in error handling
cases).
So why goto is outlawed from civilized c programmers community.
is there any technical inefficiency in that.
Thank you
推荐答案
一般来说,消除goto会有一些效率低下。
为了消除goto,你经常需要实现额外的
变量或标志将信息从内部循环传送到
外部以停止中间处理,如果
你只是直接跳出一个环。此外,往往会有一些代码重复的b
代码。有些人,比如我,通过从身体中多个地方返回来作弊,违反了严格的
结构化方法。
在机器级别,几乎总是有一个执行goto功能的跳转或分支
指令。
-
Thad
In general, there is some inefficiency with the elimination of goto.
In order to eliminate goto, you often have to implement additional
variables or flags to carry information from an inner loop to the
outside in order to stop intermediate processing, which is not needed if
you simply jump directly out of a loop. Also, there tends to be some
code duplication. Some people, such as I, cheat by returning from a
function more than one place in the body, a violation of the strict
structured approach.
At the machine level, there is almost always a jump or branch
instruction that performs a goto function.
--
Thad
它不是取缔"除非你正在使用编码标准,否则b $ b禁止它。
Goto语句很容易导致意大利面条代码。真正的问题
不是goto语句本身;这是标签。每当你在代码中看到一个
标签时,就很难分辨出程序是如何获得的。
得到了它。所谓的结构化编程 intstead从一组更高级别的构造构建一个
程序结构,例如
if / then / else,循环,开关等等。
程序的结构通常对应于现实世界中建模的某个实体。 if语句对应于决策。一个
循环对应于反复做某事。一个goto语句
对应于......好吧,从程序中的一个点跳到另一个点
;它很少匹配现实世界中的任何东西(除非
程序正在建模一个有限状态机)。
小心使用它们可能很有用,特别是作为一个
替代语言中缺少的控制结构。
C没有异常处理机制,所以gotos可能很有用
用于错误处理(当某些东西出现时,b / b
出错了)。 C没有多级中断语句,所以getos
对此有用。
向后分支的goto语句,或者通过循环直接完成
的事情,几乎总是一个坏主意。
经典论文关于这个话题,从1968年开始,是Edsger W. Dijkstra的
Go To Statement Considered Harmful,可在
< http:// www。 acm.org/classics/oct95/> ;.
-
Keith Thompson(The_Other_Keith)< http://www.ghoti.net/~kst>
圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>
我们必须做点什么。这是事情。因此,我们必须这样做。
It''s not "outlawed" unless you''re working with a coding standard that
forbids it.
Goto statements can easily lead to "spaghetti code". The real problem
isn''t the goto statement itself; it''s the label. Whenever you see a
label in code, it''s very difficult to tell how the program could have
gotten there. So-called "structured programming" intstead builds a
program structure from a set of higher-level constructs, such as
if/then/else, loops, switches, and so forth.
A program''s structure often corresponds to something in the real-world
entity being modeled. An if statement corresponds to a decision. A
loop corresponds to doing something repeatedly. A goto statement
corresponds to ... well, to jumping from one point in the program to
another; it rarely matches anything in the real world (unless the
program is modeling a finite state machine).
Used with care, however, they can be useful, especially as a
substitute for a control structure that''s missing from the language.
C doesn''t have an exception handling mechanism, so gotos can be useful
for error handling (bailing out of a nested construct when something
goes wrong). C doesn''t have a multi-level break statement, so gotos
can be useful for that.
A goto statement that branches backward, or that does something that
could have been done straightforwardly with a loop, is almost always a
bad idea.
The classic essay on the topic, from 1968, is Edsger W. Dijkstra''s
"Go To Statement Considered Harmful", available at
<http://www.acm.org/classics/oct95/>.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
获取。
让我们不要忘记
结构化编程与转到语句
Let''s not forget
"Structured Programming with go to Statements"
http://portal.acm.org/citation.cfm?id=356640
这篇关于关于“goto”在C.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!