Alex Rivera | Logout

Break nested loops

Asked 2011-12-13T22:39:35.650
10

Can someone tell me how to break the main loop when I have nested loops?
Example*:

/*Main loop*/
for(int y = 0; y < 100; y+=10)
{
    /*Sub loop*/
    for (int x = 0; x < 100; x += 10)
    {
        if(x == 60) 
        { 
            //Break the main loop 
        }
    }
}

*This code do nothing, it's just an example

What should I put in the place of the "Break main loop" comment? In java there are labels which I can break (when i set a label to the main loop named "MainLoop" I can write "break MainLoop;" and it will be valid), but what can I do here?

Thanks in advise!

Edit
Report

1 Answer

2

I don't know if there's a way to break out of nested loops in C#, but allow me to suggest a workaround.

You could throw the main loop into a function and return out of that function. You can return false; to indicate a premature break and return true; to indicate that the loop went all the way through, if that matters.

answered 2011-12-13T22:43:36.833

Your Answer