C++ 如何“打破” if 语句?

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

How do I "break" out of an if statement?

c++

提问by Wes

I have a if statement that I want to "break" out of. I understand that break is only really for loops. Can anyone help?

我有一个 if 语句,我想“打破”它。我知道 break 只是真正的 for 循环。任何人都可以帮忙吗?

For those that require an example of what I'm trying to do:

对于那些需要我正在尝试做的事情的例子的人:

if( color == red )
{
...
if( car == hyundai ) break;
...
}

采纳答案by phoog

Nested ifs:

嵌套如果:

if (condition)
{
    // half-massive amount of code here

    if (!breakOutCondition)
    {
        //half-massive amount of code here
    }
}

At the risk of being downvoted -- it's happened to me in the past -- I'll mention that another (unpopular) option would of course be the dreaded goto; a break statement is just a goto in disguise.

冒着被否决的风险——过去发生在我身上——我会提到另一个(不受欢迎的)选项当然是可怕的goto;break 语句只是变相的 goto。

And finally, I'll echo the common sentiment that your design could probably be improved so that the massive if statement is not necessary, let alone breaking out of it. At least you should be able to extract a couple of methods, and use a return:

最后,我将回应大家的共同观点,即您的设计可能会得到改进,因此不需要大量的 if 语句,更不用说打破它了。至少你应该能够提取几个方法,并使用返回:

if (condition)
{
    ExtractedMethod1();

    if (breakOutCondition)
        return;

    ExtractedMethod2();
}

回答by Matt Phillips

if (test)
{
    ...
    goto jmp;
    ...
}
jmp:

Oh why not :)

哦为什么不:)

回答by Matt Phillips

You probably need to break up your if statement into smaller pieces. That being said, you can do two things:

您可能需要将 if 语句分解成更小的部分。话虽如此,你可以做两件事:

  • wrap the statement into do {} while (false)and use real break(not recommended!!! huge kludge!!!)

  • put the statement into its own subroutine and use returnThis may be the first step to improving your code.

  • 将语句包装成do {} while (false)并使用真实的break(不推荐!!!巨大的杂物!!!)

  • 将语句放入自己的子程序中并使用return这可能是改进代码的第一步。

回答by Nim

I don't know your test conditions, but a good old switchcould work

我不知道你的测试条件,但一个好的老人switch可以工作

switch(colour)
{
  case red:
  {
    switch(car)
    { 
      case hyundai: 
      {
        break;
      }
      :
    }
    break;
  }
  :
}

回答by DipSwitch

You can't break break out of an if statement, unless you use goto.

除非使用 goto,否则无法从 if 语句中中断。

if (true)
{
      int var = 0;
      var++;
      if (var == 1)
          goto finished;
      var++;
}

finished:
printf("var = %d\n", var);

This would give "var = 1" as output

这将使“var = 1”作为输出

回答by Mark Ransom

The ||and &&operators are short circuit, so if the left side of ||evaluates to trueor the left side of &&evaluates to false, the right side will not be evaluated. That's equivalent to a break.

||&&运营商都短路,因此,如果左侧||的计算结果为true或左侧&&的计算结果为假,则右侧不会进行评价。这相当于休息。

回答by Emmanuel N

Have a labelat a point you want to jump to and in side your if use goto

在你想要跳转到的点和旁边有一个标签,如果使用goto

if(condition){
     if(jumpCondition) goto label
}
label:

回答by dasblinkenlight

There's always a gotostatement, but I would recommend nesting an ifwith an inverse of the breaking condition.

总是有一个gotostatement,但我建议使用if与破坏条件相反的嵌套 an 。

回答by Daniel Gabriel

You could use a label and a goto, but this is a bad hack. You should consider moving some of the stuff in your if statement to separate methods.

您可以使用标签和goto,但这是一个糟糕的技巧。您应该考虑将 if 语句中的一些内容移动到单独的方法中。

回答by Daniel Gabriel

You can use goto, return, or perhaps call abort (), exit ()etc.

您可以使用gotoreturn或或者叫abort ()exit ()等。