Possible to use break for outer loop?

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

break only breaks the loop that is directly around it. You could use a flag to control the outer loop:

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

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

Yes, it is possible by using a break label:

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

Tags:

C#

Java