Skip to main content
17 votes
Accepted

Best practice to "continue" from inside a nested loop?

Don't nest: convert to functions instead. And have those functions return true if they perform their action and the subsequent steps can be skipped; false otherwise. That way you completely avoid the ...
David Arno's user avatar
  • 39.6k
13 votes

Best practice to "continue" from inside a nested loop?

From a more bird view perspective, I would refactor the code so that it looks like this... (in pseudo code, it's too long ago I touched C++) void filterStrings(sl) { /* Filter string list */ ...
dagnelies's user avatar
  • 5,503
10 votes

Best practice to "continue" from inside a nested loop?

I really like how @dagnelies starts. Short and to the point. A good use of high level abstraction. I'm only tweaking it's signature and avoiding a needless negative. void ParsingTools::filterStrings(...
candied_orange's user avatar
4 votes

Best practice to "continue" from inside a nested loop?

Just use a lambda for the predicate, and then use the power of standard algorithms and short-circuiting. No need for any convoluted or exotic control-flow: void ParsingTools::filterStrings (...
Deduplicator's user avatar
  • 9,309
3 votes

Simple tic-tac-toe GUI in Common Lisp: Avoid using Continuation Passing Style?

I've written quite a bit of code like what you post and now that I'm older (wiser is debatable, I'm sure) I've realized that there was one key tension in designs like this. On the one hand you have ...
Michael's user avatar
  • 6,487
1 vote

Best practice to "continue" from inside a nested loop?

Several answers suggest a major refactor of the code. This is probably not a bad way to go, but I wanted to provide an answer which is more in line with the question itself. Rule #1: Profile before ...
Cort Ammon's user avatar
  • 11.9k
1 vote

Best practice to "continue" from inside a nested loop?

I think @dganelies has the right idea as a starting point, but I think I'd consider going a step further: write a generic function that can carry out the same pattern for (almost) any container, ...
Jerry Coffin's user avatar
  • 44.8k
1 vote

Best practice to "continue" from inside a nested loop?

There is also the option to make the content of the outer loop (the one you want to continue) a lambda, and simply use return. It's surprisingly easy if you know lambdas; you basically start your loop ...
Aganju's user avatar
  • 1,473
1 vote

Are first-class continuations useful in modern object-oriented programming languages?

The article referenced in the accepted answer is mostly true about the fact of the difficulties on reading/reasoning the CPS'd code by humans, but it is not quite convincing as the answer to this ...
FrankHB's user avatar
  • 121

Only top scored, non community-wiki answers of a minimum length are eligible