立即退出 C++ 中的“while”循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/872996/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:41:51  来源:igfitidea点击:

Immediate exit of 'while' loop in C++

c++while-loopbreak

提问by Meir

How do I exit a whileloop immediately without going to the end of the block?

如何while立即退出循环而不去到块的末尾?

For example,

例如,

while (choice != 99)
{
    cin >> choice;
    if (choice == 99)
        //Exit here and don't get additional input
    cin>>gNum;
}

Any ideas?

有任何想法吗?

回答by Andomar

Use break?

使用休息?

while(choice!=99)
{
  cin>>choice;
  if (choice==99)
    break;
  cin>>gNum;
}

回答by akappa

cin >> choice;
while(choice!=99) {
    cin>>gNum;
    cin >> choice
}

You don't need a break, in that case.

在这种情况下,您不需要休息。

回答by Elben Shira

Use break, as such:

使用break,例如:

while(choice!=99)
{
  cin>>choice;
  if (choice==99)
    break; //exit here and don't get additional input
  cin>>gNum;
}

This works for forloops also, and is the keyword for ending a switch clause. More info here.

这也适用于for循环,并且是结束 switch 子句的关键字。更多信息在这里

回答by Danny Battison

Yes, break will work. However, you may find that many programmers prefer not to use it when possible, rather, use a conditional if statement to perform anything else in the loop (thus, not performing it and exiting the loop cleanly)

是的,休息会起作用。但是,您可能会发现许多程序员在可能的情况下不喜欢使用它,而是使用条件 if 语句在循环中执行任何其他操作(因此,不执行它并干净地退出循环)

Something like this will achieve what you're looking for, without having to use a break.

这样的事情将实现您正在寻找的东西,而无需休息。

while(choice!=99) {
    cin >> choice;
    if (choice != 99) {
        cin>>gNum;
    }
}

回答by Charlie Martin

break;.

break;.

while(choice!=99)
{
   cin>>choice;
   if (choice==99)
       break;
   cin>>gNum;
}

回答by Remus Rusanu

hmm, break?

嗯,break

回答by Will Hartung

while(choice!=99)
{
  cin>>choice;
  if (choice==99)
    exit(0);
  cin>>gNum;
}

Trust me, that will exit the loop. If that doesn't work nothing will. Mind, this may not be what you want...

相信我,这将退出循环。如果那不起作用,什么都不会。注意,这可能不是你想要的......

回答by Chris Stryker

Yah Im pretty sure you just put

是的,我很确定你只是把

    break;

right where you want it to exit

就在你希望它退出的地方

like

喜欢

    if (variable == 1)
    {
    //do something
    }
    else
    {
    //exit
    break;
    }

回答by Dario

Try

尝试

break;

回答by AlbertS

You should never use a break statement to exit a loop. Of course you can do it, but that doesn't mean you should. It just isn't good programming practice. The more elegant way to exit is the following:

您永远不应该使用 break 语句退出循环。当然你可以做到,但这并不意味着你应该这样做。这不是好的编程习惯。更优雅的退出方式如下:

while(choice!=99)
{
    cin>>choice;
    if (choice==99)
        //exit here and don't get additional input
    else
       cin>>gNum;
}

if choice is 99 there is nothing else to do and the loop terminates.

如果选择为 99,则无需执行其他操作,循环终止。