如果我有一个嵌套在另一个循环中的for循环,我如何才能以最快的方式有效地从两个循环(内部和外部)中出来?

我不想必须使用布尔值然后必须说转到另一个方法,而只是在外部循环之后执行第一行代码。

有什么又快又好的方法呢?

我在想异常并不便宜/只应该在真正异常的情况下抛出等等。因此,从性能的角度来看,我不认为这个解决方案是好的。

我不认为利用。net中的新特性(anon方法)来做一些非常基础的事情是正确的。


当前回答

最干净、最短、最可重用的方法是自调用匿名函数:

没有转到 没有标签 没有临时变量 没有命名函数

用匿名方法,比上面的答案短一行。

new Action(() =>
{
    for (int x = 0; x < 100; x++)
    {
        for (int y = 0; y < 100; y++)
        {
            return; // exits self invoked lambda expression
        }
    }
})();
Console.WriteLine("Hi");

其他回答

可以使用循环中的自定义条件来打破循环,从而允许有干净的代码。

    static void Main(string[] args)
    {
        bool isBreak = false;
        for (int i = 0; ConditionLoop(isBreak, i, 500); i++)
        {
            Console.WriteLine($"External loop iteration {i}");
            for (int j = 0; ConditionLoop(isBreak, j, 500); j++)
            {
                Console.WriteLine($"Inner loop iteration {j}");

                // This code is only to produce the break.
                if (j > 3)
                {
                    isBreak = true;
                }                  
            }

            Console.WriteLine("The code after the inner loop will be executed when breaks");
        }

        Console.ReadKey();
    }

    private static bool ConditionLoop(bool isBreak, int i, int maxIterations) => i < maxIterations && !isBreak;   

在这段代码中,我们包含以下输出:

外部循环迭代0 内循环迭代0 内循环迭代1 内循环迭代2 内循环迭代3 内循环迭代4 内部循环之后的代码将在中断时执行

有时候,将代码抽象到它自己的函数中,而不是使用早期返回(早期返回是邪恶的:)

public void GetIndexOf(Transform transform, out int outX, out int outY)
{
    outX = -1;
    outY = -1;

    for (int x = 0; x < Columns.Length; x++)
    {
        var column = Columns[x];

        for (int y = 0; y < column.Transforms.Length; y++)
        {
            if(column.Transforms[y] == transform)
            {
                outX = x;
                outY = y;

                return;
            }
        }
    }
}

c#自适应的方法经常在C中使用——设置外循环的变量值的循环外条件(即对于循环使用int变量INT_MAX -1通常是很好的选择):

for (int i = 0; i < 100; i++)
{
    for (int j = 0; j < 100; j++)
    {
        if (exit_condition)
        {
            // cause the outer loop to break:
            // use i = INT_MAX - 1; otherwise i++ == INT_MIN < 100 and loop will continue 
            i = int.MaxValue - 1;
            Console.WriteLine("Hi");
            // break the inner loop
            break;
        }
    }
    // if you have code in outer loop it will execute after break from inner loop    
}

正如代码中的注释所说,break不会神奇地跳转到外循环的下一个迭代-所以如果你的代码在内循环之外,这种方法需要更多的检查。在这种情况下考虑其他解决方案。

这种方法适用于for和while循环,但不适用于foreach。在foreach的情况下,你不会有代码访问隐藏的枚举器,所以你不能改变它(即使你可以IEnumerator没有一些“MoveToEnd”方法)。

内联评论的作者致谢: i = INT_MAX - 1的建议由Meta 对于ygoe的评论。 适当的IntMax由jmbpiano 对内循环后代码的注释

在外环使用合适的防护。在你破坏之前,在内环设置保护。

bool exitedInner = false;

for (int i = 0; i < N && !exitedInner; ++i) {

    .... some outer loop stuff

    for (int j = 0; j < M; ++j) {

        if (sometest) {
            exitedInner = true;
            break;
        }
    }
    if (!exitedInner) {
       ... more outer loop stuff
    }
}

或者更好的是,将内部循环抽象为一个方法,并在外部循环返回false时退出外部循环。

for (int i = 0; i < N; ++i) {

    .... some outer loop stuff

    if (!doInner(i, N, M)) {
       break;
    }

    ... more outer loop stuff
}

这里没有提到的另一种方法是将双循环合并为产品上的单个循环,它既干净又不依赖于更新的。net特性。然后在循环内部,计数器的值可以使用简单的数学计算:

int n; //set to max of first loop
int m; //set to max of second loop

for (int k = 0; k < n * m; k++)
{
    //calculate the values of i and j as if there was a double loop
    int i = k / m;
    int j = k % m;
    
    if(exitCondition)
    {
        break;
    }
}