34

If I use break like the code below, then the loop within row won't iterate the rest if there is a match in the beginning, but what about the col loop?

Would it still iterate between 0 and 7? Is there a way to use break there as well?

for (int col = 0; col < 8; col ++)
    for (int row = 0; row < 8; row ++)
        if (check something)
        {
            //Then do this;
            break;
        }
4

13 回答 13

89

One option is to use a condition flag. You could then either break in the outer loop as well, or just use it as an extra condition within the for loops:

bool keepGoing = true;

for (int col = 0; col < 8 && keepGoing; col++)
{
    for (int row = 0; row < 8 && keepGoing; row++)
    {
        if (something)
        {
             // Do whatever
             keepGoing = false;
        }
    }
}

In Java, you can specify a label to break to though. (I didn't see that this question was tagged Java as well as C#.)

outerLoop:
for (...)
{
    for (...)
    {
        if (...)
        {
            break outerLoop;
        }
    }
}

EDIT: As noted in comments, in C#, you could use a label and goto:

for (...)
{
    for (...)
    {
        if (...)
        {
            goto endOfLoop;
        }
    }
}
endOfLoop:
// Other code

I'd really recommend that you don't take either of these approaches though.

In both languages, it would usually be best to simply turn both loops into a single method - then you can just return from the method:

public void doSomethingToFirstOccurrence()
{
    for (...)
    {
        for (...)
        {
            if (...)
            {
                return;
            }
        }
    }
}
于 2013-05-27T09:13:03.777 回答
15

是的,可以通过使用break标签:

package others;

public class A {

    public static void main(String[] args) {
        outer: for(int col = 0; col < 8; col ++)
        {
            for (int row = 0; row < 8; row ++)
            {
                if (col == 4)
                {
                    System.out.println("hi");
                    break outer;
                }
            }
        }
    }
}
于 2013-05-27T09:13:49.363 回答
5

You can put logic like this:

boolean condition = false;

for (int col = 0; col < 8; col ++)
    for (int row = 0; row < 8; row ++)
        if (check something) {
            // Then do this:
            condition = true; // Break condition for outer loop
            break;
        }
     }
     if (condition)
         break;
 }
于 2013-05-27T09:13:01.310 回答
3

It doesn't exit the col loop.

Instead, you can wrap all in a function and use return; to exit immediately from the loop

private Xy Loop( /* Parameters */)
    for (int col = 0; col < 8; col ++)
        for (int row = 0; row < 8; row ++)
            if (check something) {
                // Then do this;
                return something; //Or just return;
            }
        }
    }
}
于 2013-05-27T09:13:06.330 回答
3
nameHere:
for (yourForLoop) {
    for (anotherLoop) {
        if(condition) {
            break nameHere;
        }
    }
}
于 2013-05-27T09:17:09.187 回答
3

break只会打破直接围绕它的循环。您可以使用标志来控制外循环:

boolean continueOuterLoop = true;

for(int col = 0; continueOuterLoop && col < 8; col ++) {
    for(int row = 0; row < 8; row ++) {
        if(check something) {
            //Then do this;
            continueOuterLoop = false;
            break;
        }
    }
}
于 2013-05-27T09:15:43.917 回答
1

在 Java 中,您可以使用break label.

outer: 
for (int col = 0; col < 8; col ++)
    for (int row = 0; row < 8; row ++)
        if (check something)
        {
            break outer;
        }
    }
}

而且,由于尚未有人提及它,因此在 C# 中,您可以使用goto label.

for (int col = 0; col < 8; col ++)
    for (int row = 0; row < 8; row ++)
        if (check something)
        {
            goto outside;
        }
    }
}
outside:
于 2013-05-27T21:18:31.690 回答
1

其他答案的另一种选择是将计数器设置为最大值,以停止循环。

for (int col = 0; col < 8; col ++)
    for (int row = 0; row < 8; row ++)
        if (check something)
        {
            // Use the col and row here.

            // Now we go for a totally break of all loops.
            // To stop the loops you can set that to the maximum
            // of your loop test.
            row = 8;
            col = 8;
        }

该技巧的优点是您无需向完整循环添加任何额外的检查代码,这使其速度更快。

于 2013-05-27T19:44:51.667 回答
1

我认为您应该使用标签或标签,例如“outerLoop”。这适用于Java:

outerLoop:
    for (int col = 0; col < 8; col ++)
        for (int row = 0; row < 8; row ++)
            if (check something)
            {
                //Then do this;
                break outerLoop;
            }
于 2013-05-27T13:11:34.990 回答
0

设置 col = 8 然后中断到内部循环。

于 2013-05-27T11:28:01.340 回答
0
Loop1: 
for (int col = 0; col < 8; col ++)
{
    for (int row = 0; row < 8; row ++)
    {
        if (condition)
        {
            break Loop1;
        }
    }
}

这可以做你需要的。

于 2013-05-28T07:44:58.123 回答
0

有几种方法可以做到这一点。一种方法是在外循环中设置变量的最大值。

int maxcol = 8;
for (int col = 0; col < maxcol; col++)
{
    for (int row = 0; row < 8; row++)
    {
        if (check something)
        {
            Then do this;

            // cause the outer loop to break:
            col = maxcol;
            // break the inner loop
            break;
        }
    }
}
于 2013-05-27T09:21:32.740 回答
-1

我们可以使用标志变量的概念:

flag = 1;
for (int col = 0; col < 8; col ++)
{
    if (flag == 1)
    {
        for (int row = 0; row < 8; row ++)
        {
            if (flag == 1)
            {
                if (check something)
                {
                    //Then do this;
                    flag = 0;
                }
            }
        }
    }
}
于 2013-05-30T06:33:18.380 回答