forward break label
How can i do forward jumps like this?? Eclipse is complaining label1 is not found...
Thx
public class foo {
int xyz() {
int b = 1;
开发者_StackOverflow if (b == 0) {
break label1;
}
// MORE CODE HERE
label1:
return 1;
}
}
You are trying to use the equivalent of goto
in Java. You can't, and for good reason. Abandon ship.
Labels are included in Java for the sole reason of choosing which loop or switch to break out of, in the case of nested loops (or switch statements). They have no other purpose, and even that single purpose is often considered dangerously close to a goto.
Labels are only applicable to loops (and blocks in general). And you are trying to mimic a goto
. Don't.
You can't do that. You can only break out of an enclosing loop structure. You don't have a loop structure at all. Try this instead:
public class foo {
int xyz() {
int b = 1;
boolean skip = false;
if (b == 0) {
skip = true;
}
if (!skip) {
// MORE CODE HERE
}
return 1;
}
}
I addition to the previous answers, why not just
if (b == 0) {
return 1;
}
?
精彩评论