简体   繁体   English

C# - while 循环内的 foreach 循环 - 跳出 foreach 并立即继续 while 循环?

[英]C# - foreach loop within while loop - break out of foreach and continue on the while loop right away?

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;我忽略了如果我不调用continue; on the while loop.在while循环上。

Sorry, I didn't realize I used the word "something" twice.抱歉,我没有意识到我用了两次“某事”这个词。

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();
   }
}
while (something)
{
   foreach (var x in xs)
   {
       if (something is true)
       {
           //Break out of this foreach
           //AND "continue;" on the while loop.
           break;
       }
   }
}

If I understand you correctly, you can use the LINQ Any / All predicate here:如果我理解正确,您可以在此处使用 LINQ Any / All谓词:

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.
   }
}

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.一旦您需要break以通过嵌套结构进行传播,这种代码就会经常发生。 Whether it is a code smell or not is up for debate:-)是否是代码异味还有待商榷:-)

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

however, wouldn't both of these values always equate to true???但是,这两个值不总是等于 true 吗?

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM