Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何中断嵌套的foreach循环,然后转到c上的父foreach循环#_C#_Loops_If Statement_Foreach_While Loop - Fatal编程技术网

C# 如何中断嵌套的foreach循环,然后转到c上的父foreach循环#

C# 如何中断嵌套的foreach循环,然后转到c上的父foreach循环#,c#,loops,if-statement,foreach,while-loop,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

我有以下代码:

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
循环,直到条件为非真


提前谢谢

直接执行此操作的唯一方法是使用
goto

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

类似这样:

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

如前所述,我还建议重新考虑代码,看看是否绝对有必要将3个循环嵌套在另一个循环中。 如果是,我假设有一些逻辑涉及,你应该考虑分裂成子函数(如建议)< /P> 对于简单的代码解决方案:

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
    }
}
还没有人提到过它(Henk简要地提到过),但是最好的方法是将循环移动到它自己的方法中,并使用
return

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

正如我看到的,你接受了这个人提到你的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;
    }
}

简单明了。:)

在我看来,放入一个方法和return就像一个递归函数..不会
中断帮助?@rapsalands
break
将只退出当前循环,在这种情况下,最内层的
foreach
。OP想要退出两个内部循环,即最内部的
foreach
while
。正如我所说的,我的大学老讲师明确表示,他会让任何使用
goto
:)的人失败。我想我应该做第二个选择,我应该把while和foreach放在一个新方法上。谢谢你的建议@赛斯:那讲师是个白痴。作为一个简单的例子,使用C进行清理的惯用方法。毫无疑问,各种C邮件列表上的新程序员都试图反驳它,因为他们对goto有偏见,然后有人不得不解释为什么会有这种习惯用法。我不喜欢goto。在教学生时,避免使用它们是一个很好的方法。关键是让他们学会使用适当的循环结构,并思考程序流。经过几年的实践,开发人员学习了一种风格,并知道何时使用goto是合适的。叫老师的名字也没有什么帮助。想想看,早期我们有一位C语言老师,他使用return语句(函数的中间部分)时,任何人都失败了,这使得代码更难看,但肯定教会了我们逻辑。我想我可以有把握地说,当C#带着
yield
声明来的时候,他死于心脏病发作。我发布了同样的代码,在最佳服务中排名第一,这要归功于你。但是出于对所有神圣事物的热爱,在你的代码前面加上
//这里是龙
,这样新手程序员就不会认为这是编写代码的正确方式谢谢你的回答。然而,如果你读了我对这个答案的评论,我遵循了他的第二个建议,其中只是把它放在了一个新的方法上。:)我很高兴。。。我没有读你的评论,没关系,我告诉过你我的建议,现在由你决定……)