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

Started by
16 comments, last by 21st Century Moose 8 years ago

C++ desperately needs conditional come-froms

openwar - the real-time tactical war-game platform

Advertisement

Put it in the loop statement rather than use break.


for( a = a_init; a != a_end && !condition; a = a_next )
  for( b = b_init; b != b_end && !condition; b = b_next )
    ...

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

A billion times this. Putting the extra condition in the loop is clean and easy to understand.

If you've got a deeply nested loop, you may have to rethink what you're doing instead of blaming the language. If impossible to refactor, breaking the algo into several functions is a perfectly valid strategy. Gets easier to grok, may even reveal more info thanks to isolation and dependencies thanks to the function arguments being passed.

I strongly disagree with the advice of using a boolean variable. It's a much worse way to express control flow than any of the alternatives. Moving the loop into a separate function should be done if it makes sense (you can give it a good name, it has a well-defined interface... that kind of thing), not because you are trying to avoid writing `goto' at all costs. That being said, it often does make sense to move the loop into a separate function.

A billion times this. Putting the extra condition in the loop is clean and easy to understand.


You can't break in the middle of a block though:

doSomething ();

if (condition) break;

doSomethingElse ();

Well you could, by introducing an else, but then you're starting to lose clarity. On balance, return is absolutely unambiguous about what the programmer's intentions are and has the lowest associated risk if someone revisits the code in 6 months and tries to get too clever with it.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

This is one of a handful of cases where goto is still relevant. As was already mentioned, break is just a special case of goto.

You could use a lamda and return instead, but a good compiler is going to produce identical machine code for both cases, and it doesn't provide a readability benefit.

Hope C++17 adds break N like in PHP. would be nice.

This is only case where I use goto and there is nothing wrong about it.

You can use lambda. You create inline lambda, make it capture everything by reference with [&] and call it immediately.

When you call return, you will return from whole lambda.


// some preceding code

// our double loop
[&]()
{
   for a ......
      for b .....
         if (some_condition)
            return;
}(); // calling lambda immediately

// some following code

I'd just break it out into a separate function, to be honest. The compiler's going to inline it anyway if it determines that there's a perf benefit, and in the event that I ever need the code twice, the work is already done for me.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

This topic is closed to new replies.

Advertisement