For faster navigation, this Iframe is preloading the Wikiwand page for goto.

goto

GOTO是一条可以在许多电脑编程语言中找到的语句。它是英文单词goto的组合。当执行这条语句的时候,它将控制流程无条件地转到另一条语句(也叫“跳转”)。跳转语句需要指明标记,在不同语言中,标记可以是标识符行号。在机器码级别,goto是一种分支的形式。

在一些语言中,可以不用显示地使用goto关键字而实现同样的功能,例如breakcontinue关键字可以跟随一个标识符。SNOBOL语言支持一种语句的后缀形式,可以在本条语句执行完毕后实现无条件跳转。

GOTO语句被大部分高级语言支持,只有很少的高级语言不支持GOTO语句。例如,goto是一个Java语言的保留字,但是不允许使用。

用法

[编辑]

goto语句通常和IF语句一起使用来实现一个条件跳转。

   IF 条件 THEN goto 标签;

编程语言一般对goto语句跳转的位置加以严格限制。例如,在C中,不允许跳转至另一个函数中的标记位置。[1]Setjmp函数提供了对非本地goto的支持。

例子

[编辑]

以下C++例子中,我们要在二维数组ar中找寻有没有100这个数。如果找到,要立即停止搜索,节省时间。

for (int i=0; i<n; i++)
    for (int j=0; j<m; j++)
        if (ar[i][j]==100)goto found;
...

found:
...

如果使用break只能跳出目前所在的一个for循环,因此需要使用goto跳出所有循环。

对于goto使用的批评

[编辑]

GOTO语句一直是批评和争论的目标,主要的负面影响是使用GOTO语句使程序的可读性变差,甚至成为不可维护的“面条代码”。随着结构化编程在二十世纪六十年代到七十年代变得越来越流行,许多电脑科学家得出结论,即程序应当总是使用被称为“结构化”控制流程的命令,如循环以及if-then-else语句来替代GOTO。甚至在今天,许多程序风格编码标准禁止使用GOTO语句。为GOTO语句辩护的人认为,加以限制地使用GOTO语句不会导致低质量的代码,并且声称在许多编程语言中,一些任务如果不使用一条或多条GOTO语句是无法被直接实现的。如有限状态自动机的实现、跳出嵌套循环以及异常处理

大概最著名的对于GOTO的批评是艾兹格·迪杰斯特拉在1968年的一篇名称为《GOTO语句有害论》的论文。[2]迪杰斯特拉认为不加限制地使用GOTO语句应当从高级语言中废止,因为它使分析和验证程序正确性(特别是涉及循环)的任务变得复杂。另外一种观点出现在高德纳Structured Programming with go to Statements [3]中,文章分析了许多常见编程任务,然后发现其中的一些使用GOTO将得到最理想的结构。

这些批评在一些编程语言的设计上起到了效果。虽然Ada语言的设计者在二十世纪七十年代晚期意识到了对于GOTO的批评,这条语句仍旧被包含进去,主要是用来支持自动生成那些goto语句必不可少的代码。[4]但是,作为goto语句目的地的标签必须使用双尖括号括起来(如:<<Start_Again>>),而这个语法在其他语言中都不被使用。这使得检查程序中goto目的地的存在变得容易。goto语句本身使用简单的形式goto Start_Again;.

变体

[编辑]

有许多不同的语言构成可以看作是goto的变形:

限制的GOTO

[编辑]

许多语言,如C语言Java,提供了相关的控制流语句,如breakcontinue,它们都是有效地被限制的goto语句。它们的作用是无条件跳转,但是只能够跳到循环块结束的位置——继续进入下一循环(continue)或者结束循环(break)。对于break语句,部分语言允许附加特定参数来控制结束循环的范围,例如PHP允许标记数字参数代表跳出嵌套语句的层数,[5]Java允许例如在语句块开始前添加<标签名>:,在语句块内以标签名作为参数代表跳转到该循环语句的结束位置。

对于Java而言,虽然goto是作为保留字,但没有赋予其功能,从而限制goto的使用。

switch/case结构

[编辑]

C语言C++Java中的switch语句高效地实现了一个多路goto,跳转目标由表达式的值来选择。

参见

[编辑]

参考资料

[编辑]
  1. ^ C Standard section 6.8.6.1 The goto statement. [2009-05-16]. (原始内容存档于2007-12-24). 
  2. ^ Edsger Dijkstra. Go To Statement Considered Harmful. Communications of the ACM. March 1968, 11 (3): 147–148. doi:10.1145/362929.362947. 
  3. ^ Donald Knuth. Structured Programming with go to Statements (PDF). Computing Surveys. 1974, 6 (4): 261–301 [2009-05-16]. doi:10.1145/356635.356640. (原始内容 (PDF)存档于2009-08-24). 
  4. ^ John Barnes英语John Barnes (computer scientist). Programming in Ada 2005. Addison Wesley. 2006-06-30: 114–115. ISBN 0-32-134078-7. 
  5. ^ PHP: break - Manual. www.php.net. [2018-09-14]. (原始内容存档于2021-01-28) (英语). 

外部链接

[编辑]
{{bottomLinkPreText}} {{bottomLinkText}}
goto
Listen to this article

This browser is not supported by Wikiwand :(
Wikiwand requires a browser with modern capabilities in order to provide you with the best reading experience.
Please download and use one of the following browsers:

This article was just edited, click to reload
This article has been deleted on Wikipedia (Why?)

Back to homepage

Please click Add in the dialog above
Please click Allow in the top-left corner,
then click Install Now in the dialog
Please click Open in the download dialog,
then click Install
Please click the "Downloads" icon in the Safari toolbar, open the first download in the list,
then click Install
{{::$root.activation.text}}

Install Wikiwand

Install on Chrome Install on Firefox
Don't forget to rate us

Tell your friends about Wikiwand!

Gmail Facebook Twitter Link

Enjoying Wikiwand?

Tell your friends and spread the love:
Share on Gmail Share on Facebook Share on Twitter Share on Buffer

Our magic isn't perfect

You can help our automatic cover photo selection by reporting an unsuitable photo.

This photo is visually disturbing This photo is not a good choice

Thank you for helping!


Your input will affect cover photo selection, along with input from other users.

X

Get ready for Wikiwand 2.0 🎉! the new version arrives on September 1st! Don't want to wait?