每个人都知道Dijkstra 给编辑的信件:转到被认为有害的声明(也就是这里 .html脚本和这里的 .pdf)并且从那时起就有一个强大的推动,尽可能避开goto声明.虽然可以使用goto来生成不可维护的,庞大的代码,但它仍然保留在现代编程语言中.甚至Scheme中的高级连续控制结构也可以被描述为复杂的goto.
什么情况下可以使用goto?什么时候最好避免?
作为后续问题:C提供了一对函数setjmp和longjmp,它们不仅可以在当前堆栈帧内,而且可以在任何调用帧中进行转换.这些应该被视为像goto一样危险吗?更危险吗?
Dijkstra本人对这个头衔表示遗憾,他对此不负责任.在EWD1308(也是这里 .pdf)结束时,他写道:
最后是一个短篇小说的记录.1968年,ACM的通讯以" 被认为有害的goto声明 "的标题发表了我的一篇文章,遗憾的是,在后来的几年中,这一期刊最常被引用,但是,经常被作者看到的不多于标题,通过成为一个模板成为我的名声的基石:我们会看到几乎任何X的标题"X被认为有害"的各种文章,包括一个标题为"Dijkstra被认为有害"的文章.但是发生了什么?我提交了一份题为" 反对goto声明的案件 "的论文"为了加快出版速度,编辑已经变成了一封"给编辑的信",并在此过程中给了他一个新的自己的发明名称!编辑是Niklaus Wirth.
关于这个主题的经过深思熟虑的经典论文,与Dijkstra的相关,是结构化编程,由Donald E. Knuth撰写.阅读都有助于重新建立背景和对主题的非教条性理解.在本文中,Dijkstra对此案的观点得到了报道,甚至更为强烈:
Donald E. Knuth:我相信通过提出这样一种观点,我实际上并不同意Dijkstra的观点,因为他最近写了以下内容:"请不要陷入相信我非常悲惨的陷阱.去发言.我有其他人正在一个宗教出来的不舒服的感觉,就好像编程的概念问题可以通过一个单一的技巧来解决,通过编码规则的简单形式! "
任何人都可以解释我究竟在哪里setjmp(),longjmp()功能可以在嵌入式编程中实际使用?我知道这些是用于错误处理的.但我想知道一些用例.
我想在捕获异常后再次执行try块中的代码.这有可能吗?
对于Eg:
try
{
//execute some code
}
catch(Exception e)
{
}
Run Code Online (Sandbox Code Playgroud)
如果异常被捕获,我想再次进入try块以"执行一些代码"并再次尝试执行它.
如何在不同的函数中使用goto函数.例如,
main()
{
....
REACH:
......
}
void function()
{
goto REACH ;
}
Run Code Online (Sandbox Code Playgroud)
如何实现这样的用法?
gotoing is evil and obsolete, please read the justification of why it is viable in this case. Before you mark it as duplicate, please read the full question.I was reading about virtual machine interpreters, when I stumbled across computed gotos . Apparently they allow significant performance improvement of certain pieces of code. The most known example is the main VM interpreter loop.
Consider a (very) simple VM like this:
#include <iostream> …Run Code Online (Sandbox Code Playgroud) 这是我知道的唯一两种大致有意义的方法:
for (){
for (){
if (found the target) goto cont;
}
}
cont:;
Run Code Online (Sandbox Code Playgroud)
我见过人们强烈建议不要使用goto,但我觉得这是一种非常正确的使用方式。
bool exit=false;
for (){
for (){
if (found the target){
exit=true;
break;
}
}
if (exit) break;
}
Run Code Online (Sandbox Code Playgroud)
我用这种方式多写了一些代码。
您认为执行此操作的正确方法是什么?
我想通过检查相同方法中的条件来跳过一些代码行。我使用了 goto 语句,但它显示“无法从此 goto 语句跳转到标签”。有没有其他方法可以跳过代码?我做的是..
if(condition)
goto skipped;
//code to skip
//code to skip
skipped:
//code to execute
Run Code Online (Sandbox Code Playgroud) 现在大多数人不在goto他们的代码中使用语句。好的。在 SO 中,有许多线程在goto使用,所有线程都“允许”它退出深度循环。
展示一些:
我查看了cppreference.com,其中显示:
当无法使用其他语句将控制转移到所需位置时使用。
敲响我警钟的是“否则不可能”
goto任何机构都可以举出一些无法避免的例子吗?
注意:我将此问题标记为“C++”,只是因为我对 C++ 示例更感兴趣。任何其他语言对我都有好处。
#include <iostream>
#include <cstdlib>
using namespace std;
main()
{
beginning:
string name;
cout << "Hey! Enter your name" << endl;
cin >> name;
int i = name.length() - 1;
do
{
cout << name[i];
i--;
} while (i > -1);
cout << " Your reverse name is " << name[i] << endl;
cout << name[i] << " Your reverse name is " << endl;
goto beginning;
}
Run Code Online (Sandbox Code Playgroud)
cout<<" Your reverse name is "<<name[i]<<endl;我最大的问题如上所述,无法跳转到fin标签(第27行错误),错误:从此处(第12和14行错误)和交叉初始化错误(第20行错误),请帮忙!
#include <iostream>
#include <string>
int main()
{
std::string name;
std::cout << "Please comply. y/n: ";
std::string answer;
std::cin >> answer;
if (answer == "y"){std::cout << "You were spared." << std::endl; goto fin;}
if (answer == "Miche"){std::cout << "The killers understood that you understood the prophecy, so they took you to their master" << std::endl; goto secret;}
if (answer == "n"){std::cout << "You were brutally killed." << std::endl; goto fin;}
else {std::cout << "You randomly babled " << answer << …Run Code Online (Sandbox Code Playgroud) c++ ×6
goto ×5
c ×2
.net ×1
c# ×1
exception ×1
objective-c ×1
optimization ×1
reverse ×1
stdstring ×1