Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iterator for loops with break

Tags:

java

let say my code look like below

for(..)
  for(..)
    for(..){
            break;  //this will break out from the most inner loop OR all 3 iterated loops?
    }
like image 449
cometta Avatar asked Nov 24 '09 10:11

cometta


People also ask

Can I use break to stop for loop?

break terminates the execution of a for or while loop. Statements in the loop after the break statement do not execute.

Can you use break in for loop Java?

We can use Java break statement in all types of loops such as for loop, while loop and do-while loop.

How do you break a loop?

To break out of a for loop, you can use the endloop, continue, resume, or return statement.


1 Answers

Your example will break out of the innermost loop only. However, using a labeled break statement, you can do this:

outer:
  for(..)
    for(..)
      for(..){
        break outer;  //this will break out from all three loops
      }
like image 165
Greg Hewgill Avatar answered Oct 14 '22 03:10

Greg Hewgill