tags:

views:

111

answers:

4

Any example please?

+1  A: 
  public static void main(String[] args)
  {
    hello: break hello;
  }
eljenso
+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
w00t, learned some new Java.
Adrian
How about using LABEL wihout being in the defined LABEL. Like if(i ==0){ continue label1;} else {//do something}label1: {//do something}
Optimize Prime
I don't think this is possible. It is actually a disguised goto, which isn't allowed in Java.
kgiannakakis
That terribly sucks then..
Optimize Prime
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
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
How about using LABEL wihout being in the defined LABEL. Like if(i ==0){ continue label1;} else {//do something}label1: {//do something}
Optimize Prime