Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Continue in nested while loops

In this code sample, is there any way to continue on 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;        }    } } 
like image 592
SkunkSpinner Avatar asked Jul 15 '09 19:07

SkunkSpinner


People also ask

Does continue work in nested loops?

When continue statement is used in a nested loop, it only skips the current execution of the inner loop. Java continue statement can be used with label to skip the current iteration of the outer loop too.

How do I use continue in nested loops?

The continue statement is used to start the next iteration of a loop,skipping everything in the loop,after the continue . In your case,once the execution of the program reaches the continue statement,then the next iteration of your inner loop starts,skipping whatever there was after the continue .

How do you continue a while loop?

The continue statement passes control to the next iteration of the nearest enclosing do , for , or while statement in which it appears, bypassing any remaining statements in the do , for , or while statement body.

How do you continue in a nested while loop in Python?

If the continue statement is present in a nested loop, it skips the execution of the inner loop only. The “continue” is a reserved keyword in Python. Generally, the continue statement is used with the if statement to determine the condition to skip the current execution of the loop.


1 Answers

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


"continue" and "break" are nothing more than a pleasant syntax for a "goto". Apparently by giving them cute names and restricting their usages to particular control structures, they no longer draw the ire of the "all gotos are all bad all the time" crowd.

If what you want to do is a continue-to-outer, you could simply define a label at the top of the outer loop and then "goto" that label. If you felt that doing so did not impede the comprehensibility of the code, then that might be the most expedient solution.

However, I would take this as an opportunity to consider whether your control flow would 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))     {  // 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 to a method:

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

Second, can all the loops be eliminated? If you are looping because you are trying to search for something, then refactor 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 loops then there is no need to break or continue!

like image 65
Eric Lippert Avatar answered Oct 05 '22 15:10

Eric Lippert