0

Below will be compilation fail due to "label z is missing" but if I just move z: to one step below after o = o + 2 then that will work? What is the logic behind this?

public class Breaker {
static String o = "";

public static void main(String[] args) {
z: 
o = o + 2;
for (int x = 3; x < 8; x++) {
    if (x == 4)
    break;
    if (x == 6)
    break z;
    o = o + x;
}
System.out.println(o);
}
}
Ketan Bhavsar
  • 5,338
  • 9
  • 38
  • 69
  • 1
    read the documentation it clearly explains the same http://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html – AurA May 21 '13 at 09:40
  • just move `o = o + 2` above `z:`, it should work. because `z` should represent a loop – hamid May 21 '13 at 09:42

1 Answers1

2

You cannot put the labels everywhere in the code. it should be only before statements. in this case labelname: for(;;){} Here's the documentation

Laksitha Ranasingha
  • 4,321
  • 1
  • 28
  • 33