Jump to content
  • Advertisement
Sign in to follow this  
Norman Barrows

C++ best way to break out of nested for loops

This topic is 922 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

C++    best way to break out of nested for loops

 

 

for a ......

    for b .....

         if (some_condition)

              // break out of both loops

 

 

 

 

a break will get you out of the inner loop.

 

but i think only goto will get you out of both, without returning that is.

 

 

Share this post


Link to post
Share on other sites
Advertisement


Or break the algorithm out into it's own function and use return.

 


return always seems cleanest to me.

 

yeah, that's what i usually do.

 

but its just a stupid little nested loop to iterate over a collision map and find the first passable node for use as a test goal for the new A* code in Caveman 3.0.

 

i'll make it a function call with return.

 

its faster than looking up goto syntax, and cleaner than stuffing it into the for() statement.

 

some "patterns" never change, eh?

Share this post


Link to post
Share on other sites

Lol yeah you'd never write either of those two bits of code, hopefully  :lol:

The lambda syntax comes in handy when you want to reuse a bit of code, but don't want to bother actually breaking it out into it's own independent function, yet. e.g.

void test()
{
  int sum = 0;
  auto SumArray = [&](int* data, int size){
    for( int i=0; i!=size; ++i )
      sum += data[i];
  };
  for( int widgetIdx=0; widgetIdx != numWidgets; ++widgetIdx )
    SumArray( widgets[widgetIdx].data, widgets[widgetIdx].size );
  SumArray( specialWidget.data, specialWidget.size );
  printf( "%d\n", sum );
}

which is shorthand for:

void test()
{
  int sum = 0;
  struct _ { _(int& sum):sum(sum){}int& sum;
    void operator()(int* data, int size){
      for( int i=0; i!=size; ++i )
        sum += data[i];
    };
  }SumArray(sum);
  for( int widgetIdx=0; widgetIdx != numWidgets; ++widgetIdx )
    SumArray( widgets[widgetIdx].data, widgets[widgetIdx].size );
  SumArray( specialWidget.data, specialWidget.size );
  printf( "%d\n", sum );
}

or the more obvious, actually breaking it out into it's own separate function:

namespace { void SumArray(int* data, int size, int& sum){
  for( int i=0; i!=size; ++i )
    sum += data[i];
};}
void test()
{
  int sum = 0;
  for( int widgetIdx=0; widgetIdx != numWidgets; ++widgetIdx )
    SumArray( widgets[widgetIdx].data, widgets[widgetIdx].size, sum );
  SumArray( specialWidget.data, specialWidget.size, sum );
  printf( "%d\n", sum );
}

Share this post


Link to post
Share on other sites

 

some "patterns" never change, eh?

We get new tools though :)
void test()
{
  printf( "start\n" );
  for( int x=0; x!=10; ++x )
    for( int y=0; y!=10; ++y )
      if( x == y && x == 5 )
        goto double_break;
      else
        printf( "%d, %d\n", x, y );
  double_break:
  printf( "end\n" );
}
In 2016 can be written as:
void test()
{
  printf( "start\n" );
  [](){
  for( int x=0; x!=10; ++x )
    for( int y=0; y!=10; ++y )
      if( x == y && x == 5 )
        return;
      else
        printf( "%d, %d\n", x, y );
  }();
  printf( "end\n" );
}

Did not realize you could invoke the lambda at its definition.

 

"its faster than looking up goto syntax"

If you've ever written a goto, how could you possibly need to look up the syntax? 

 

I'd be looking up the break syntax if anything, to see if C++ features labeled breaks yet.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!