How do I break out of nested loops in Java?
Like other answerers, I'd definitely prefer to put the loops in a different method, at which point you can just return to stop iterating completely. This answer just shows how the requirements in the question can be met.
You can use break
with a label for the outer loop. For example:
public class Test {
public static void main(String[] args) {
outerloop:
for (int i=0; i < 5; i++) {
for (int j=0; j < 5; j++) {
if (i * j > 6) {
System.out.println("Breaking");
break outerloop;
}
System.out.println(i + " " + j);
}
}
System.out.println("Done");
}
}
This prints:
0 0
0 1
0 2
0 3
0 4
1 0
1 1
1 2
1 3
1 4
2 0
2 1
2 2
2 3
Breaking
Done
Java- breaking out of an inner loop
I think you have a logic error, given that i = 3
board[i][j] + 1
will be equal to board[3][j] + 1
I think what you meant to do was this:
public static int[][] reshuffle(int[][] board) {
int count1 = 0;
int count2 = 0;
int count3 = 0;
int count4 = 0;
for(int i=0;i<4;i++) {
for (int j = 0; j < 14; j++) {
if (i==0 && board[i][j] + 1 != board[0][j + 1]) {
count1 = j+1;
break;
} else if (i==1 && board[i][j] + 1 != board[1][j + 1]) {
count2 = j+1;
break;
} else if (i==2 && board[i][j] + 1 != board[2][j + 1]) {
count3 = j+1;
break;
} else if (i==3 && board[i][j] + 1 != board[3][j + 1]) {
count4 = j+1;
break;
}
}
}
}
How do exit two nested loops?
In Java you can use a label to specify which loop to break/continue:
mainLoop:
while (goal <= 100) {
for (int i = 0; i < goal; i++) {
if (points > 50) {
break mainLoop;
}
points += i;
}
}
Break statement inside two while loops
In your example break statement will take you out of while(b) loop
while(a) {
while(b) {
if(b == 10) {
break;
}
}
// break will take you here.
}
Breaking out of nested for loops in Java
Use a label such as :
outer:
while(condition) {
// want to return to this point
for (Integer x : xs) {
// point 1
for (Integer y : ys) {
// point 2
...
}
...
}
for (Integer a : as) {
for (Integer b : bs) {
// point 3
...
}
...
}
}
and you can then use break outer;
to escape the while loop. This works with nested for loops as well, but I try not to overuse labels
As pointed out by @Peter, use continue outer;
if you wish to finish the current outer iteration early and continue on to the next, as opposed to escaping the while loop.
Does the break statement break out of multiple loops?
break;
only breaks from the innermost loop or switch
body it appears in.
If you intend to break from nested loops, you might consider moving the nested loops to a separate function and using return
to exit the function from any point inside its body.
Example:
...
int matrix[ROWS][COLS];
int value;
...
int found = 0;
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
if (matrix[row][col] == value) {
found = 1;
break;
}
}
if (found)
break;
}
...
Can be simplified as:
int hasvalue(int matrix[ROWS][COLS], int value) {
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
if (matrix[row][col] == value)
return 1;
}
}
return 0;
}
...
int matrix[ROWS][COLS];
int value;
...
found = hasvalue(matrix, value);
...
What's the best way to break from nested loops in JavaScript?
Just like Perl,
loop1:
for (var i in set1) {
loop2:
for (var j in set2) {
loop3:
for (var k in set3) {
break loop2; // breaks out of loop3 and loop2
}
}
}
as defined in EMCA-262 section 12.12. [MDN Docs]
Unlike C, these labels can only be used for continue
and break
, as Javascript does not have goto
.
How do I break out of nested loops in Java?
Like other answerers, I'd definitely prefer to put the loops in a different method, at which point you can just return to stop iterating completely. This answer just shows how the requirements in the question can be met.
You can use break
with a label for the outer loop. For example:
public class Test {
public static void main(String[] args) {
outerloop:
for (int i=0; i < 5; i++) {
for (int j=0; j < 5; j++) {
if (i * j > 6) {
System.out.println("Breaking");
break outerloop;
}
System.out.println(i + " " + j);
}
}
System.out.println("Done");
}
}
This prints:
0 0
0 1
0 2
0 3
0 4
1 0
1 1
1 2
1 3
1 4
2 0
2 1
2 2
2 3
Breaking
Done
Related Topics
Java Equivalent to PHP'S Preg_Replace_Callback
Working Post Multipart Request With Volley and Without Httpentity
Should I Actually Remove the Valueeventlistener
Java.Lang.Classnotfoundexception: Didn't Find Class on Path: Dexpathlist
C++ Equivalent of Java'S Instanceof
How to Access the Java Method in a C++ Application
Is There a Java Equivalent or Methodology For the Typedef Keyword in C++
Remove HTML Tags from a String
What Are the Pros and Cons of the Leading Java HTML Parsers
How to Efficiently Parse HTML With Java
Inconsistent Performance Applying Foregroundactions in a Jeditorpane When Reading Html
Convert Word Doc to HTML Programmatically in Java
Why Is It Bad Practice to Call System.Gc()
Swing Gui Listeners Without Awt