Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
for an example
{
if( color == red )
...
if( car == hyundai ) break;
...
}
Posted

The only way you have to 'break' an if statement (that is exiting the if statement block) is using goto. However that is probably a symptom of ugly design.
 
Share this answer
 
v2
If you have a code block (e.g. the block of an if-statement) that is relatively long and you want to break out of it at several places, you may use the following construct:

C++
do {
   ... some code ...
   if (...)
       break;
   ... some more code ...
   if (...)
       break;
   ... some more code ...
} while (0);

The do-block is guaranteed to be executed just once and you can break out of it in the usual way with break.

However, consider that such constructs are kind of a last resort. It's probably better to structure your code in a way that you don't need to resort to such "tricks".
 
Share this answer
 
v2
Technically every control flow statement is equivalent, i. e. you can rewrite any code that uses one control flow statement in such a way that it uses another control flow statement instead, while arriving at the same result. Not that it would always be a good idea ;)

In your example, if the outer loop is a loop, you can use continue instead of break. However, as this would just lead to jump forward to the next iteration of the loop, you also need to remember the fact that you wish to exit the loop and check this in your loop condition:
C++
bool end_of_loop = false;
...
do {
   ...
   // some code
   ...
   if (...) {
      end_of_loop = true;
      continue;
   }
   ...
   // some more code
   ...
} while (!end_of_loop);

This code will work for any kind of loop: do, for or while, but not other types of code blocks.

Also it really is no improvement over break - neither from the point of readability nor performance, so if your goal is to avoid goto-like commands, you can instead use if inside your code as a means to skip over the rest of the code block:
C++
bool end_of_loop = false;
...
do {
   ...
   // some code
   ...
   if (...) {
      end_of_loop = true;
   }
   if (!end_of_loop) {
      ...
      // some more code
      ...
   }
} while (!end_of_loop);

This will work in any kind of code block, not just loops.
 
Share this answer
 
Put your goal path clear, everything else is a break (for your whole path) :)
C++
long example(..)
{
  long lResult(0);

  if (..) {
    ..
    if (..) {
      ..
      if (..) {
        ..
        if (..) {
          ..
          if (..) {
            ..
            lResult = 1;
          }
          // no Options here
        }
        // no Options here
      }
      // no Options here
    }
    // no Options here
  }
  return lResult;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900