7
while (foo() == true)
{
   foreach (var x in xs)
   {
       if (bar(x) == true)
       {
           //"break;" out of this foreach
           //AND "continue;" on the while loop.
       }
   }

   //If I didn't continue, do other stuff.
}

I'm a bit stuck on how to do this.


Update: I fixed the question. I left out the fact that I need to process other stuff if I don't call a continue; on the while loop.

Sorry, I didn't realize I used the word "something" twice.

8
  • 1
    just break; the while will continue because something is true Commented Jul 15, 2011 at 16:06
  • Sorry, I left something out. I need to be able to process additional items if I don't continue;. Commented Jul 15, 2011 at 16:08
  • then you need to either write if(something)continue; when you exit the foreach loop, or use goto label Commented Jul 15, 2011 at 16:11
  • 1
    Then edit the question so that it is clear! This question is very unclear. You are using "something" to mean two different things, it is very unclear where the necessary side effects are in these loops, and so on. It might be possible to rewrite this whole thing with no loops at all, or to refactor it into methods, or whatever, but without knowing what the actual problem is, it is difficult to say. Commented Jul 15, 2011 at 16:17
  • Oh, oops. I didn't even realize I used something twice. Commented Jul 15, 2011 at 16:20

6 Answers 6

14

I would rewrite this:

while (foo() == true)
{
   foreach (var x in xs)
   {
       if (bar(x) == true)
       {
           //"break;" out of this foreach
           //AND "continue;" on the while loop.
       }
   }

   //If I didn't continue, do other stuff.
   DoStuff();
}

as

while (foo()) // eliminate redundant comparison to "true".
{
   // Eliminate unnecessary loop; the loop is just 
   // for checking to see if any member of xs matches predicate bar, so
   // just see if any member of xs matches predicate bar!
   if (!xs.Any(bar))        
   {
       DoStuff();
   }
}
Sign up to request clarification or add additional context in comments.

4 Comments

This is nicer than what I was trying to do. :)
@myermian: A good rule of thumb that I try to use: if a loop is for executing a particular side effect over and over again, then use a loop. If a loop is solely for computing a value, then move the loop into a helper function, or find a helper function that already does what you want -- like "Any", or "All", or "Sum", or whatever. That way you make the loop an implementation detail of the helper, rather than a nested control flow of your method.
Yea, I didn't even think about the Any or All functions. But, it ended up helping a lot. I flipped the if so it continues if it matches any and does the DoStuff() otherwise (Resharper agreed that inversing the if helps avoid nesting). This is way cleaner than having a bool value, setting it, breaking out of the for loop, checking the bool to see what the result was...
@myermian / eric: Out of curiosity, in what way is this different from my answer?
6
while (something)
{
   foreach (var x in xs)
   {
       if (something is true)
       {
           //Break out of this foreach
           //AND "continue;" on the while loop.
           break;
       }
   }
}

5 Comments

Sorry, I left something out. I need to be able to process additional items if I don't continue;.
Smells like refactoring needed - I think you need to look at the conditional logic you're using...
sorry, i didn't understand your comment or update - do you want to stay in the while after hitting the break?
Yea, I messed up by using the term "something" twice.
but hitting the break in the for loop will continue the while loop
4

If I understand you correctly, you can use the LINQ Any / All predicate here:

while (something)
{
    // You can also write this with the Enumerable.All method
   if(!xs.Any(x => somePredicate(x))
   {
      // Place code meant for the "If I didn't continue, do other stuff."
      // block here.
   }
}

Comments

2

This should address your requirement:

while (something)
{   
    bool doContinue = false;

    foreach (var x in xs)   
    {       
        if (something is true)       
        {           
            //Break out of this foreach           
            //AND "continue;" on the while loop.          
            doContinue = true; 
            break;       
        }   
    }

    if (doContinue)
        continue;

    // Additional items.
}

This sort of code happens frequently as soon as you need break to propagate through nested constructs. Whether it is a code smell or not is up for debate :-)

Comments

0
while (something)
{
   foreach (var x in xs)
   {
       if (something is true)
       {
           break;
       }
   }
}

however, wouldn't both of these values always equate to true???

Comments

0

So you want to continue after breaking?

while (something)
{
    bool hit = false;

    foreach (var x in xs)
    {
        if (something is true)
        {
            hit = true;
            break;
        }
    }

    if(hit)
        continue;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.