Follow up to this page: Breaking out of nested loops in Java
This code works just fine (part of a Sudoku solver, so p is a 9x9 table):
int r = 0, c = 0;
out:
for(r = 0; r < 9; ++r){
for (c = 0; c < 9; ++c){
if (p[r][c] == 0){
break out;
}
}
}
// do stuff with r, c
But this code fails! The only change is that the 'init' sections of the for loops are empty.
int r = 0, c = 0;
out:
for( ; r < 9; ++r){
for ( ; c < 9; ++c){
if (p[r][c] == 0){
break out;
}
}
}
// processes first row of array as it should, then breaks out with r=9, c=9
Since r
and c
are defined and initialized above the loops, these blocks ought to do exactly the same thing, but they don't. Anyone have any idea why this behaves the way it does?