简体   繁体   English

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

[英]How to break nested foreach loop then go to parent foreach loop on c#

I have the following code: 我有以下代码:

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

What I want to do is when I hit the 2nd if statement on the last foreach loop is to return on the first foreach loop. 我想要做的是当我点击第二个if语句时,最后一个foreach循环是在第一个foreach循环返回。

Note: If the 2nd if statement is not true, it should continue the last foreach loop until the condition is not true. 注意:如果第二个if语句不为true,它应该继续最后一个foreach循环,直到条件不为真。

Thanks in advance! 提前致谢!

The only way to this directly is with a goto . 直接的唯一方法是使用goto

Another (better) option is to restructure until the problem goes away. 另一个(更好的)选择是重组,直到问题消失。 For instance by putting the inner code (while + foreach) in a method and use return to get back. 例如,将内部代码(while + foreach)放在方法中并使用return返回。

Something like this: 像这样的东西:

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

Noone has mentioned it (Henk mentioned briefly) yet but the best approach would be to move your loops into its own method and use return 没人提到它(Henk简要提到),但最好的方法是将循环移动到自己的方法并使用return

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

As i see you accepted the answer in which the person refers you goto statement, where in modern programming and in expert opinion goto is a killer, we called it a killer in programming which have some certain reasons, which i will not discuss it over here at this point, but the solution of your question is very simple, you can use a Boolean flag in this kind of scenario like i will demonstrate it in my example: 当我看到你接受了这个人引用你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;
    }
}

simple and plain. 简单明了。 :) :)

As said before me, I also recommend to re-factor the code and see if it is absolutely necessary to have 3 loops nested one in another. 如前所述,我还建议重新考虑代码,看看是否有必要将3个循环嵌套在另一个中。 If it is, and I assume there is some logic involve, you should consider splitting into sub functions (as suggested) 如果是,并且我假设有一些逻辑涉及,你应该考虑拆分为子函数(如建议的那样)

For a simple code solution: 对于简单的代码解决方案:

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

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

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