C# - 在while循环中使用foreach循环 - 如何在foreach循环中跳出并立即继续执行while循环?

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

我有点卡在如何做这件事情上。


更新:我修复了问题。我遗漏了一个事实,如果我不在while循环中调用continue;,我需要处理其他东西。

抱歉,我没有意识到我两次使用了单词“something”。


1
只需break;,while循环将继续,因为something is true - Djole
抱歉,我漏掉了一些东西。如果我不使用 continue;,我需要能够处理其他的项目。 - myermian
然后,当您退出foreach循环时,您需要编写if(something)continue;或使用goto label - Djole
1
然后编辑问题以使其清晰明了!这个问题非常不清楚。您使用“something”来表示两个不同的事物,很难确定这些循环中必要的副作用在哪里,等等。也许可以完全重写整个内容而不需要任何循环,或者将其重构为方法等,但是如果不知道实际问题是什么,就很难说了。 - Eric Lippert
foobar仅用于它们的值,还是它们也具有副作用? - Eric Lippert
显示剩余3条评论
6个回答

14
我会重写这段内容:

我会对此进行改写:

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

这比我尝试做的更好。 :) - myermian
4
一个我试图使用的好的经验法则是: 如果一个循环用于反复执行特定副作用,则使用循环。如果循环仅用于计算一个值,则将循环移入帮助函数中,或者找到已经实现你想要的功能的帮助函数,比如"任意"(Any)、"所有"(All)、"求和"(Sum),等等。这样你就可以将循环作为帮助函数的实现细节,而不是你方法中的嵌套控制流程。 - Eric Lippert
是的,我甚至没有考虑到Any或All函数。但是,它最终帮了很大的忙。我翻转了if语句,如果匹配任何一个就continue,否则执行DoStuff()(Resharper同意反转if可以避免嵌套)。这比拥有一个bool值、设置它、跳出for循环、检查bool以查看结果要干净得多... - myermian
@myermian / eric:出于好奇,这种方法与我的答案有何不同? - Ani

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

抱歉,我漏掉了一些东西。如果我不使用 continue;,我需要能够处理其他的项目。 - myermian
需要重构 - 我认为你需要查看你正在使用的条件逻辑... - FiveTools
抱歉,我不理解你的评论或更新 - 你想在跳出循环后继续停留在 while 循环中吗? - adrianos
是的,我犯了错,使用了两次“something”这个术语。 - myermian
但在for循环中使用break语句将会继续执行while循环。 - adrianos

4

如果我理解你的意思正确,您可以在这里使用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.
   }
}

2

这应该可以满足你的需求:

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

这种代码经常出现在你需要通过嵌套结构传播 break 的情况下。它是否是一种代码异味还有待商榷 :-)

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

然而,这两个值不总是等于真吗???


0

你想在中断后继续执行吗?

while (something)
{
    bool hit = false;

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

    if(hit)
        continue;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接