0

Possible Duplicate:
Breaking out of a nested loop

I have two for loops like this:

for (...)
{
   // some code

   for (...)
   {
       if (something)
       {
            // go to the outer for loop   <<<<<< 
       }
   }

   // some more code here 
}

If I use continue it will go to the inner loop. I want it to go to the outer one.
How can I do that?

Community
  • 1
  • 1
Bohn
  • 26,091
  • 61
  • 167
  • 254

4 Answers4

8

Use break. As the documentation says:

The break statement terminates the closest enclosing loop or switch statement in which it appears. Control is passed to the statement that follows the terminated statement, if any.

So your code should look like this:

Outer For-Loop
{
   ...// some code
   Inner For-Loop
   {
       if( something  = true )
       {
            break;//breaks out of inner loop
       }
   }

   // some more code here 
}
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
3

did you mean you want "break"???

vcsjones
  • 138,677
  • 31
  • 291
  • 286
Leonardo
  • 10,737
  • 10
  • 62
  • 155
3

Using break instead of continue?

Tobia Zambon
  • 7,479
  • 3
  • 37
  • 69
1

this is another way to do it

one: for( ){

   for( ){

        if(  ){

          continue one
       }  
   } 

}

use the tag "name:" I used one:

when the condition is met then the code "continue one" will jump to the first for