如何打破嵌套的foreach循环然后转到c#上的父foreach循环

时间:2013-07-11 10:18:40

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

我有以下代码:

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

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

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

提前致谢!

6 个答案:

答案 0 :(得分:31)

直接的唯一方法是使用goto

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

答案 1 :(得分:15)

这样的事情:

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

答案 2 :(得分:7)

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

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

答案 3 :(得分:5)

正如我所见,你接受了这个人给你转到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;
    }
}
简单明了。 :)

答案 4 :(得分:4)

如前所述,我还建议对代码进行重新分解,看看是否有必要将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
    }
}

答案 5 :(得分:3)

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;
             }
        }
    }
}
相关问题