【问题标题】:How to break nested foreach loop then go to parent foreach loop on c#如何打破嵌套的foreach循环然后转到c#上的父foreach循环
【发布时间】:2013-07-09 13:47:31
【问题描述】:

我有以下代码:

foreach(// Some condition here)
{
    while (// Some condition here)
    {
        foreach (// Some condition here)
        {
             if (// Condition again)
             {
                  //Do some code
             }
             if (// Condition again)
             {
                 //Stop the first foreach then go back to first foreach
             }
        }
    }
}

我想要做的是,当我在最后一个 foreach 循环上点击第二个 if 语句时,是在第一个 foreach 循环上返回。

注意:如果第二个if 语句不为真,它应该继续最后一个foreach 循环,直到条件不为真。

提前致谢!

【问题讨论】:

  • 放入一个方法并返回
  • 在我看来像一个递归函数..
  • 不会break; 帮忙吗?
  • @rapsalands break 只会退出当前循环,在这种情况下是最里面的foreach。 OP想要退出两个内部循环,即最里面的foreachwhile

标签: c# loops if-statement foreach while-loop


【解决方案1】:

直接解决此问题的唯一方法是使用goto

另一个(更好的)选择是重组直到问题消失。例如,通过将内部代码(while + foreach)放入方法中并使用 return 返回。

【讨论】:

  • 就像一个注释,我的老大学讲师明确表示他会让任何使用 goto 的人失败:)
  • 我认为我应该做第二个选项,我应该将 while 和 foreach 放在一个新方法上。感谢您的建议!
  • @Sayse 那位讲师是个白痴。举个简单的例子,在 C uses gotos 中进行清理的惯用方式。由于对 goto 的偏见,各种 C 邮件列表上的新程序员都会尝试反对它,然后有人必须解释为什么存在这个习语。
  • 我不喜欢 goto 的。在教学生时,避免使用它们是一个好方法。重点是让他们学会使用正确的循环结构并思考程序流程。经过几年的实践,开发人员学会了一种风格并知道什么时候使用 goto 是合适的。叫老师的名字也没有用。
  • 想一想,我们早期有一个 C 语言老师,他让任何使用 return 语句中间函数的人都失败了),这使得代码更难看,但绝对教会了我们逻辑。我想我可以肯定地说,当 C# 带有 yield 声明时,他死于心脏病发作。
【解决方案2】:

类似这样的:

resetLoop = false;
for(// Some condition here)
{
    while (// Some condition here)
    {
        foreach (// Some condition here)
        {
             if (// Condition again)
             {
                  //Do some code
             }
             if (// Condition again)
             {
                 //Stop the first foreach then go back to first foreach
                 resetLoop = true;
                 break;
             }
        }
        if (resetLoop) break;
    }
    if (resetLoop) {
        // Reset for loop to beginning
        // For example i = 0;
    }
}

【讨论】:

  • 我发布了相同的代码,最好的服务,感谢你。但出于对所有神圣事物的热爱,请在您的代码前加上//Here be dragons,这样新手编码人员就不会认为这是正确的编码方式
【解决方案3】:

还没有人提到它(Henk 简要提到过),但最好的方法是将你的循环移动到它自己的方法中并使用 return

public ReturnType Loop(args)
{
foreach outerloop
    foreach innerLoop
       if(Condition)
          return;
}

【讨论】:

    【解决方案4】:

    我看到你接受了这个人提到你的 goto 语句的答案,在现代编程和专家意见中 goto 是一个杀手,我们称它为编程中的杀手,有一些特定的原因,我不会讨论此时就到这里了,但是您的问题的解决方案非常简单,您可以在这种情况下使用布尔标志,就像我将在我的示例中演示的那样:

    foreach(// Some condition here)
    {
        //solution
        bool breakme = false;
    
        while (// Some condition here)
        {
            foreach (// Some condition here)
            {
                if (// Condition again)
                {
                    //Do some code
                }
                if (// Condition again)
                {
                    //Stop the first foreach then go back to first foreach
                    breakme = true;
                    break;
                }
            }
        }
        if(breakme)
        {
            break;
        }
    }
    

    简单明了。 :)

    【讨论】:

    • 感谢您的回答。但是,如果您阅读了我对该答案的评论,我会遵循他的第二个建议,即把它放在一个新方法上。
    • :) 很高兴...我没有阅读您的评论,没关系,我告诉了您我的建议,现在由您决定... :)
    【解决方案5】:

    正如我之前所说,我还建议重构代码,看看是否绝对有必要让 3 个循环相互嵌套。 如果是,并且我认为其中涉及一些逻辑,您应该考虑拆分为子函数(如建议的那样)

    对于一个简单的代码解决方案:

    foreach(// Some condition here)
    {
        var broke = false;
        while (// Some condition here)
        {
            foreach (// Some condition here)
            {
                 if (// Condition again)
                 {
                      //Do some code
                 }
                 if (// Condition again)
                 {
                     //Stop the first foreach then go back to first foreach
                     broke = true;
                     break;
                 }
            }
            if (broke) break;
            // continue your while loop
        }
    }
    

    【讨论】:

      【解决方案6】:
      var broken = false;
      foreach(// Some condition here)
      {
          broken = false;
          while (!broken && // Some condition here)
          {
              foreach (// Some condition here)
              {
                   if (// Condition again)
                   {
                       //Do some code
                   }
                   if (// Condition again)
                   {
                       //Stop the first foreach then go back to first foreach
                       broken = true;
                       break;
                   }
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多