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

C++

C++ Problem Overview


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

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

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

C++ Solutions


Solution 1 - C++

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.

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 (condition)
{
    ExtractedMethod1();

    if (breakOutCondition)
        return;

    ExtractedMethod2();
}

Solution 2 - C++

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

Oh why not :)

Solution 3 - C++

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

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

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

Solution 4 - C++

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

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

Solution 5 - C++

There's always a goto statement, but I would recommend nesting an if with an inverse of the breaking condition.

Solution 6 - C++

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.

Solution 7 - C++

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

Solution 8 - C++

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

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

Solution 9 - C++

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

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

Solution 10 - C++

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWesView Question on Stackoverflow
Solution 1 - C++phoogView Answer on Stackoverflow
Solution 2 - C++Matt PhillipsView Answer on Stackoverflow
Solution 3 - C++user3458View Answer on Stackoverflow
Solution 4 - C++NimView Answer on Stackoverflow
Solution 5 - C++Sergey KalinichenkoView Answer on Stackoverflow
Solution 6 - C++Daniel GabrielView Answer on Stackoverflow
Solution 7 - C++Mark RansomView Answer on Stackoverflow
Solution 8 - C++DipSwitchView Answer on Stackoverflow
Solution 9 - C++Emmanuel NView Answer on Stackoverflow
Solution 10 - C++user405725View Answer on Stackoverflow