【问题标题】:C# - foreach loop within while loop - break out of foreach and continue on the while loop right away?C# - while 循环中的 foreach 循环 - 跳出 foreach 并立即继续 while 循环?
【发布时间】:2023-04-09 01:32:01
【问题描述】:
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;,我忽略了我需要处理其他内容的事实。

抱歉,我没有意识到我用了两次“某事”这个词。

【问题讨论】:

  • 只是break; 会继续,因为something is true
  • 抱歉,我遗漏了一些内容。如果我不continue;,我需要能够处理其他项目。
  • 那么你需要在退出foreach循环时写if(something)continue;,或者使用goto label
  • 然后编辑问题,使其清楚!这个问题非常不清楚。您使用“某物”来表示两种不同的事物,非常不清楚这些循环中必要的副作用在哪里,等等。完全可以不使用循环重写整个事情,或者将其重构为方法或其他任何东西,但不知道实际问题是什么,很难说。
  • 哦,哎呀。我什至没有意识到我使用了 something 两次。

标签: c# for-loop while-loop break continue


【解决方案1】:

我会重写这个:

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

作为

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:我尝试使用的一个很好的经验法则:如果循环用于一遍又一遍地执行特定的副作用,则使用循环。如果循环仅用于计算值,则将循环移动到辅助函数中,或者找到一个已经完成您想要的辅助函数 - 例如“Any”或“All”,或者“总和”之类的。这样,您就可以使循环成为帮助程序的实现细节,而不是方法的嵌套控制流。
  • 是的,我什至没有考虑 Any 或 All 功能。但是,它最终帮助了很多。我翻转了 if 所以它 continues 如果它匹配任何并且 DoStuff() 否则(Resharper 同意反转 if 有助于避免嵌套)。这比拥有一个 bool 值、设置它、跳出 for 循环、检查 bool 以查看结果如何...
  • @myermian / eric:出于好奇,这与我的回答有何不同?
【解决方案2】:

如果我理解正确,您可以在此处使用 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.
   }
}

【讨论】:

    【解决方案3】:

    所以你想在中断后继续?

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

    【讨论】:

      【解决方案4】:

      这应该满足您的要求:

      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 通过嵌套结构进行传播时,此类代码会经常发生。是否是代码异味还有待商榷:-)

      【讨论】:

        【解决方案5】:
        while (something)
        {
           foreach (var x in xs)
           {
               if (something is true)
               {
                   break;
               }
           }
        }
        

        但是,这两个值不总是等于 true 吗???

        【讨论】:

          【解决方案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;,我需要能够处理其他项目。
          • 闻起来像需要重构——我认为你需要看看你正在使用的条件逻辑......
          • 抱歉,我没听懂你的评论或更新 - 你想在休息后留下一会儿吗?
          • 是的,我把“某事”这个词用了两次,搞砸了。
          • 但在 for 循环中打断 继续 while 循环
          猜你喜欢
          • 1970-01-01
          • 2013-10-01
          • 1970-01-01
          • 2014-10-19
          • 2016-03-14
          • 2015-07-03
          • 2017-05-28
          • 2014-12-05
          • 2012-10-02
          相关资源
          最近更新 更多