C语言 如何跳回程序顶部
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19160205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to jump back to the top of a program
提问by Fox-3
I have a large block of code inside main and would like to return to the top of main and run through again if a certain variable in 'code' is true. How would I do this?
我在 main 中有一大块代码,如果“code”中的某个变量为真,我想返回到 main 的顶部并再次运行。我该怎么做?
#include <stdio.h>
void main(void)
{
// if varable in code is true return to here
//code
//
//
//
}
回答by uml?ute
int main(void)
{
int gotostart=0;
// if varable 'gotostart' in code is true return to here
beginning:
// code [...]
if(gotostart)
goto beginning;
return 0;
}
as Armen has rightly pointed out, gotodeserves some warning. the most popular ist Dijsktra's GOTO statements considered harmful, as it is somewhat counterproductive to structured programming.
正如阿尔门正确指出的那样,goto值得一些警告。最流行的 Dijsktra 的GOTO 语句被认为是有害的,因为它对结构化编程有些适得其反。
a more structured approach would be:
更结构化的方法是:
int main(void)
{
int gotostart=0;
do { // if varable 'gotostart' in code is true return to here
// code [...]
} while(gotostart)
return 0;
}
回答by unwind
Remove the code from main()and put it in a function:
删除代码main()并将其放入一个函数中:
static void my_function(void)
{
/* lots of stuff here */
}
Then just call it:
然后只需调用它:
int main(void)
{
my_function();
if(condition)
my_function();
return 0;
}
This is way cleaner than using a loop, in my opinion, since the use case was not really "loop-like". If you want to do something once or twice, break it out into a function then call the function once or twice. As a bonus, it also gives you a great opportunity to introduce a name (the function name) for the thing that your program is doing, which helps make the code easier to understand.
在我看来,这比使用循环更干净,因为用例并不是真正的“类似循环”。如果你想做一两次事情,把它分解成一个函数,然后调用这个函数一两次。作为奖励,它还为您提供了一个很好的机会来为您的程序正在做的事情引入一个名称(函数名称),这有助于使代码更容易理解。
回答by Lundin
int main (void)
{
bool keep_looping = true;
while (keep_looping)
{
// code
if(done_with_stuff)
{
keep_looping = false;
}
}
}
回答by TopGunCoder
The easiest way to achieve what you are talking about would probably to use the while loop as mentioned but do this:
实现您所谈论内容的最简单方法可能是使用上述 while 循环,但请执行以下操作:
while(true){
if(something){
continue;
} // then use break when you want to leave the loop all together
}
回答by pablo1977
If your program repeats ever the same schema, then a while()loop is the best approach.
But if your program is a little cumbersome, perhaps you would prefer a gotostatement, in order to jump to the labelyou desire:
如果您的程序重复使用相同的模式,那么while()循环是最好的方法。
但是如果你的程序有点麻烦,也许你更喜欢一个goto语句,以便跳转到你想要的标签:
int main(void) {
// Initial stuff
jump_point:
// Do more stuff
if (some-condition)
goto jump_point;
// ... ...
return 0;
}
I think you should have to design your program in a way that a natural and clear loop is executed:
我认为你应该以一种自然而清晰的循环执行的方式设计你的程序:
int main(void) {
while(terminate-condition-is-false) {
// Do all your stuff inside this loop
}
return 0;
}
回答by Rahul Vashisth
You can use a gotostatement:
您可以使用以下goto语句:
//Assume here is your starting point of your program
start: //we have declared goto statement for beginning
//Assume here is your ending point
goto start; //Here is that show go back to first position

