Any example please?
+7
A:
Here is an example of using labels and break statements without a loop:
block1: {
if (a < 0) {
break block1;
}
if (b < 0) {
break block1;
}
return a + b;
}
kgiannakakis
2009-12-21 13:04:50
w00t, learned some new Java.
Adrian
2009-12-21 14:38:28
How about using LABEL wihout being in the defined LABEL. Like if(i ==0){ continue label1;} else {//do something}label1: {//do something}
Optimize Prime
2009-12-21 15:07:29
I don't think this is possible. It is actually a disguised goto, which isn't allowed in Java.
kgiannakakis
2009-12-21 15:49:37
That terribly sucks then..
Optimize Prime
2009-12-21 16:11:05
A:
If you want some unreadable code:
int i = 1;
int j = 1;
label: switch (i) {
case 1:
switch (j) {
case 1:
break label;
}
default:
System.out.println("end");
}
Without break;
will print "end". break label;
will skip the print.
Thomas Jung
2009-12-21 13:09:04
A:
certainly:
private boolean isSafe(String data) {
validation: {
if (data.contains("voldemort")) {
break validation;
}
if (data.contains("avada")) {
break validation;
}
if (data.contains("kedavra")) {
break validation;
}
return true;
}
return false;
}
@Optimize Prime: that's not possible. You can only continue or break a label from within it's scope, for example:
label1: for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
System.out.println(i + " " + j);
if (i == j) {
continue label1;
}
}
}
produces:
0 0
1 0
1 1
2 0
2 1
2 2
3 0
3 1
3 2
3 3
Adriaan Koster
2009-12-21 14:10:34
How about using LABEL wihout being in the defined LABEL. Like if(i ==0){ continue label1;} else {//do something}label1: {//do something}
Optimize Prime
2009-12-21 15:09:13