Use a Label breakTag(s): Language
Labelled breaks allow breaking out of several levels of nested loops inside a pair of curly braces. This way, you can almost simulate a GOTO!
class JavaGoto {
public static void main(String args[]) {
int max = 10;
int limit = 5;
int j = 0;
out: {
for (int row=0; row< max; row++ ) {
for (int col=0; col< max; col++ ) {
if( row == limit) break out;
}
j += 1;
}
}
System.out.println(j); // output 5
}
}
mail_outline
Send comment, question or suggestion to howto@rgagnon.com
Send comment, question or suggestion to howto@rgagnon.com