1

Can someone tell me why the compiler says the label is missing in the below code:

CASE1:

Error: Label is missing

void crazyLoop() {
    int c = 0;
    JACK: while (c < 8) {
        JILL: System.out.println(c);
        if (c > 3)
            break JILL;
        else
            c++;
    }
}

Here JILL is reachable as I am have declared JILL inside JACK and not outside JACK.

CASE2:

Error: c cannot be resolved. And if I do this :

void crazyLoop() {
    JILL:int c = 0;
    JACK: while (c < 8) {
         System.out.println(c);
        if (c > 3)
            break JILL;
        else
            c++;
    }
}

the compiler says c cannot be resolved to a variable.

Can someone please explain what is happening ?

v1shnu
  • 2,211
  • 8
  • 39
  • 68
  • A label can only be applied to `for`, `while` and `do-while` loops, not to any arbitrary statement like your int `c = 0` -- that's just invalid syntax and the compiler tells you as much. A `break` statement terminates a loop, but does not transfer control flow to the label. What did you expect to happen in the second code sample? – Philipp Reichart Nov 10 '16 at 13:40
  • It's not entirely correct. In the first example System.out.println() has a label which is fine. – pcjuzer Nov 10 '16 at 13:47

3 Answers3

1

The problem is with JILL:int c = 0;, because it's invalid according to Java grammar. So, the error c cannot be resolved to a variable is just an effect of the first error.

You can't use label before a local variable declaration. See this answer for more details.

Community
  • 1
  • 1
pcjuzer
  • 2,724
  • 4
  • 25
  • 34
  • ok. So a label cannot be used with statements that declare variables. Can you tell me why the first case causes an error – v1shnu Nov 10 '16 at 14:01
  • @pcjuzer thanks, maybe the OP error then was also on the declaration line. – Joop Eggen Nov 10 '16 at 14:19
  • First one causes an error, because the label isn't on a loop statement somewhere around the break which refers to it. In Java you can put labels to many places but 'goto' isn't implemented, so it doesn't make sense to use them. 'break' and 'continue' is implemented for labels, but they aren't like 'goto'. You can't break out or continue to any arbitrary label. It makes sense to use labels for nested loops, if you want to break/continue an outer loop you can refer to it through a label. – pcjuzer Nov 10 '16 at 14:21
1

from JSL 14.15. The break Statement

A break statement with label Identifier attempts to transfer control to the enclosing labeled statement (§14.7) that has the same Identifier as its label

So the error is caused by break statement not referring to the label of the loop. Also, only statements may have label prefixes. see documentation

The following code compiles successfully:

 void crazyLoop() {
    int c = 0;
    ILL:
    c= 0;

JACK: while (c < 8) {
     System.out.println(c);
    if (c > 3)
        break JACK;
    else
        c++;
}
mrtexaz
  • 663
  • 7
  • 22
  • In the above case 1, the label JILL refers to a statement - System.out. But why is it saying that the Label is missing ? – v1shnu Nov 10 '16 at 14:02
  • in case 1, the label JILL is correct; but the break must refer to the enclosing labeled statement (which has label JACK); see the definition, as provided in my answer – mrtexaz Nov 10 '16 at 14:16
0

See the java reference:

The scope of a label of a labeled statement is the immediately contained Statement.

This means a label should better be placed before a compound statement like a for-loop or such.

JACK: while (c < 8) {
    JILL: System.out.println(c);
    if (c > 3)
        break JILL;
    else
        c++;
}

The scope of JILL is just System.out.println(c);. break JACK would break out of the while-loop. After the statement JILL is not known.

The second error of an unknown c seems as if the compiler encapsulates the labeled statement in { }. It could be that this is a compiler "bug" (compound statement assumption) - but maybe other readers can shed some light: trying out other compilers, finding something in the reference on declarations.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • can you please explain the statement with an example ? In my example, JILL is inside JACK and I am calling break JILL inside of JACK. So why is it not inside the scope ? – v1shnu Nov 10 '16 at 14:03