Continue in nested loops

In this code example, is there any way to continue along the outer loop from the catch block?

while { // outer loop while { // inner loop try { throw; } catch { // how do I continue on the outer loop from here? continue; } } } 
+38
c # loops while-loop continue
Jul 15 '09 at 19:20
source share
10 answers

UPDATE: This question was inspired by my article on this subject. Thanks for the great question!




"continue" and "break" are nothing more than nice syntax for "goto". Apparently, giving them cute names and restricting their use to specific control structures, they no longer gain the ire of "the crowds of all yearlings - everything is bad."

If what you want to do is a continuation-external, you can simply define a label at the top of the outer loop and then “jump” to that label. If you think that this does not interfere with the understanding of the code, this may be the most appropriate solution.

However, I would like to consider this as an opportunity to consider whether your control flow will benefit from some refactoring. Whenever I have conditional "break" and "continue" in nested loops, I consider refactoring.

Consider:

 successfulCandidate = null; foreach(var candidate in candidates) { foreach(var criterion in criteria) { if (!candidate.Meets(criterion)) // Edited. { // TODO: no point in continuing checking criteria. // TODO: Somehow "continue" outer loop to check next candidate } } successfulCandidate = candidate; break; } if (successfulCandidate != null) // do something 

Two refactoring techniques:

First, extract the inner loop into the method:

 foreach(var candidate in candidates) { if (MeetsCriteria(candidate, criteria)) { successfulCandidate = candidate; break; } } 

Secondly, is it possible to exclude all loops? If you are looping because you are trying to find something, then reorganize it into a query.

 var results = from candidate in candidates where criteria.All(criterion=>candidate.Meets(criterion)) select candidate; var successfulCandidate = results.FirstOrDefault(); if (successfulCandidate != null) { do something with the candidate } 

If there are no cycles, then there is no need to interrupt or continue!

+85
Jul 15 '09 at 19:40
source share
  while { // outer loop while { // inner loop try { throw; } catch { // how do I continue on the outer loop from here? goto REPEAT; } } // end of outer loop REPEAT: // some statement or ; } 

The problem is resolved. (what? Why are you all fooling me like that?)

+21
Jul 15 '09 at 19:43
source share

You can use the break; operator.

 while { while { try { throw; } catch { break; } } } 

Continue is used to jump to the top of the current loop.

If you need to tear out more levels, you will either have to add an if or use the scary / not recommended goto.

+17
Jul 15 '09 at 19:23
source share

Change the try / catch structure with the inner while loop:

 while { try { while { throw; } } catch { continue; } } 
+10
Jul 15 '09 at 19:22
source share

No.
I suggest extracting the inner loop into a separate method.

 while { // outer loop try { myMethodWithWhileLoopThatThrowsException() } catch { // how do I continue on the outer loop from here? continue; } } } 
+4
Jul 15 '09 at 19:23
source share

Use break in the inner loop.

+2
Jul 15 '09 at 19:23
source share

You just want to get out of the inner that will continue the outer.

 while { // outer loop while { // inner loop try { throw; } catch { // how do I continue on the outer loop from here? break; } } } 
+1
Jul 15 '09 at 19:24
source share

I think the best way to achieve this is to use the break statement. A break completes the current cycle and continues execution from where it ends . In this case, complete the inner loop and to return back to the outer loop . Here is what your code looks like:

 while { // outer loop while { // inner loop try { throw; } catch { // break jumps to outer loop, ends inner loop immediately. break; //THIS IS THE BREAK } } } 

I believe this is what you were looking for, right? Thank!

0
Jul 15 '09 at 19:35
source share
 using System; namespace Examples { public class Continue : Exception { } public class Break : Exception { } public class NestedLoop { static public void ContinueOnParentLoopLevel() { while(true) try { // outer loop while(true) { // inner loop try { throw new Exception("Bali mu mamata"); } catch (Exception) { // how do I continue on the outer loop from here? throw new Continue(); } } } catch (Continue) { continue; } } } } } 
0
Jul 23 '09 at 15:37
source share

Use your own exception type, such as MyException. Then:

 while { try { // outer loop while { // inner loop try { throw; } catch { // how do I continue on the outer loop from here? throw MyException; } } } catch(MyException) { ; } } 

This will work to continue and exit multiple levels of nested impressions. Sorry for the poor formatting;)

-2
Jul 15 '09 at 19:41
source share



All Articles