I have the following for loop :
for (int i = 0; i<arr.length;i++) {
for(int j =0; j<someArr.length; j++) {
//after this inner loop, continue with the other loop
}
}
I would like to break out of the inner loop and continue the iteration of the outer loop. How do I do this?
Generally you can use a combination of lable and break to jump to where you want like this
OUTER_LOOP: for (int i = 0; i<arr.length;i++) {
for(int j =0; j<someArr.length; j++) {
//after this inner loop, continue with the other loop
break OUTER_LOOP;
}
}
If you want to break to some where in the outer loop you do like this, put the lable where you want to jump to(outside of the current loop) and use that lable in the break statement.
for (int i = 0; i<arr.length;i++) {
//line code 1
OUTER_LOOP: // line code 2
for(int j =0; j<someArr.length; j++) {
//after this inner loop, continue with the other loop
break OUTER_LOOP;
}
}
break does not stop all iterations.
So if you do the following, you will only break out of the nested loop (second for) and continue the current iteration of the first for loop:
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < someArr.length; j++) {
break;
// NOT executed after the break instruction
}
// Executed after the break instruction
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With