摆脱多重循环?

发布于 2024-12-27 12:49:28 字数 594 浏览 5 评论 0原文

可能的重复:
打破嵌套循环

我有这段代码

foreach (___)
{
    foreach (___)
    {
        foreach (___)
        {
            if (condition)
            {
                //break out of all loops
            }
        }
    }
}

但是break< /code> 只“打破”最内部的循环(对不起我的英语),我想把它们全部留下......

我在想:

  1. GOTO,但我总是被告知不要使用它
  2. 一种布尔值-标志

是还有什么更优雅的方式吗?就像 C# 提供的那样?

预先感谢您的任何帮助!

Possible Duplicate:
Breaking out of a nested loop

I have this code

foreach (___)
{
    foreach (___)
    {
        foreach (___)
        {
            if (condition)
            {
                //break out of all loops
            }
        }
    }
}

But break only "breaks" the most inner loop (sorry for my english) and I want to leave them all...

I was thinking about :

  1. GOTO, but I've always been told to not use it
  2. A sort of boolean-flag

Is there any more elegant way ? like provided by C# ?

Thanks in advance for any help !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

白龙吟 2025-01-03 12:49:28

对此的一个非常优雅的解决方案是将整个循环嵌套移动到一个单独的方法中,并在您想要跳出所有循环时return;

A very elegant solution to this is to move the entire nest of loops to a separate method and return; when you want to break out of all loops.

絕版丫頭 2025-01-03 12:49:28

goto 在这里完全没问题:

foreach (___)
{
    foreach (___)
    {
        if (condition)
        {
            goto done;
        }
    }
}
done: ;

A goto is perfectly fine here:

foreach (___)
{
    foreach (___)
    {
        if (condition)
        {
            goto done;
        }
    }
}
done: ;
酒与心事 2025-01-03 12:49:28

最好的方法可能是重构为带有 return 语句的较小方法。但如果您需要范围内的大量变量,您始终可以使用委托。

   Action work = delegate
        {
            foreach (___)
            {
                 foreach (___)
                 {
                       foreach (___)
                       {
                            if (condition)
                            {
                                 return;
                            }
                       }
                  }
             }

        };

  work();

The best way is probably to refactor to smaller methods with return statements. But if you need a lot of variables in scope you can always use a delegate.

   Action work = delegate
        {
            foreach (___)
            {
                 foreach (___)
                 {
                       foreach (___)
                       {
                            if (condition)
                            {
                                 return;
                            }
                       }
                  }
             }

        };

  work();
丢了幸福的猪 2025-01-03 12:49:28

几年前,当我还在学校的时候,我们有一位非常愚蠢的老师教信息学。其中一条规则是“不得跳转”。我有一个具有多个循环的算法,在没有 goto 的情况下,其大小会增长到许多倍。

有充分的理由,打破多重循环就是其中之一。

Years ago, when I was in school, we had informatics with a pretty out of the books stupid teacher. One rule was "no goto". I had an algorithm with multiple loops that grew to many times the size without goto.

There are good reasons, and breaking out of multiple loops is one.

丑丑阿 2025-01-03 12:49:28

也许这是使用goto的唯一正当理由。

Maybe this is the only valid reason to use goto.

天荒地未老 2025-01-03 12:49:28
bool bLoopBreak = false;

foreach (___)
{
    foreach (___)
    {
        foreach (___)
        {
            if (condition)
            {
                bLoopBreak = true;
                break;
                //break out of all loops
            }
        }
        if (bLoopBreak) break;
    }
    if (bLoopBreak) break;
}
bool bLoopBreak = false;

foreach (___)
{
    foreach (___)
    {
        foreach (___)
        {
            if (condition)
            {
                bLoopBreak = true;
                break;
                //break out of all loops
            }
        }
        if (bLoopBreak) break;
    }
    if (bLoopBreak) break;
}
别靠近我心 2025-01-03 12:49:28

在这种情况下,不要害怕使用gotobreakContinue 只是 goto 的语法糖,并且很少被正确注释,它可以使代码比大量嵌套检查来查看是否更清晰。你应该停止循环。

Don't be afraid to use goto in a case like this. break and continue are just syntactic sugar for goto, and is sparingly and commented correctly it can make code clearer than lots of nested checks to see if you should stop looping.

清欢 2025-01-03 12:49:28

PHP 可以让您做到这一点:

foreach (___)
{
    foreach (___)
    {
        foreach (___)
        {
            break 3; // get out of all 3 loops
        }
    }
}

我不太了解 C#,但它可能有类似的功能。

PHP lets you do this:

foreach (___)
{
    foreach (___)
    {
        foreach (___)
        {
            break 3; // get out of all 3 loops
        }
    }
}

I don't know a whole lot about C# but it may have a similar feature.

秉烛思 2025-01-03 12:49:28

只是为了在那里抛出一个替代方案:您也可以在循环内抛出异常。在 C#/Java/Python 中,这样做的成本相当低。 (在 C++ 中,情况并非如此。)

请注意,Python 对于类似情况有一个 StopIteration 异常,因此它并非闻所未闻,尽管它在 C# 领域可能有点不正统。 ;)

有时,您可以免费获得例外,因此可以使用它,但如果您采用这种方法,最好将其记录下来。

try {
    foreach (___) {
        foreach (___) {
            if(your condition true)  {
                throw new IterationDone(); // or use a singleton to avoid allocating
            }
        }
    }
    // not found
catch (IterationDone e) {
    // yay    
}

我已将答案制作为社区维基,以便可以查看和讨论。 (我并不提倡将其作为第一个也是最好的解决方案,它只是一个解决方案,因此值得出现在这里......)

Just to throw an alternative in there: You could also throw an exception inside the loop. In C#/Java/Python this is reasonably cheap to do. (In C++, not so much.)

Note that Python has a StopIteration exception for a similar case, so it's not unheard of, although it might be a bit unorthodox in C#-land. ;)

Sometimes, you get the exception for free, and thus it can be used, but you'd better document it if you are taking that approach.

try {
    foreach (___) {
        foreach (___) {
            if(your condition true)  {
                throw new IterationDone(); // or use a singleton to avoid allocating
            }
        }
    }
    // not found
catch (IterationDone e) {
    // yay    
}

I've made the answer a community wiki, so that it can be seen and discussed. (I don't advocate it as the first and best solution, it's just A solution, and thus deserves to be here...)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文