void crazyLoop() { JILL: int c = 0; System.out.println(c); JACK: while (c < 8) { if (c > 3) break JACK; else c++; } }
Hi ! in the second code , you are declaring int c at scope of label JILL, so it is visible only in this label . You can not mention variable c in Label JACK ! I hope this is the problem
K Anshul
Ranch Hand
Joined: May 19, 2004
Posts: 71
posted
0
I don't think that's the problem. I thought labels work for loops only. But i first code Jill label is not used for loop then why does it work?
Chris Allen
Ranch Hand
Joined: Feb 01, 2003
Posts: 127
posted
0
Labels can be placed on any Java statement, not just looping statements. The effect of placing them on non looping constructs is the limitation of scope they apply to. I believe Victor is correct in his scope comment as from the JLS (14.7):
Since the code is not enclosed in any braces, the scope is limited to the next statement ending with the semicolon. In this case, it really doesn't offer much in terms of functionality but it is legal.
K Anshul
Ranch Hand
Joined: May 19, 2004
Posts: 71
posted
0
Originally posted by victor santos: void crazyLoop() { JILL: int c = 0; System.out.println(c); JACK: while (c < 8) { if (c > 3) break JACK; else c++; } }
Hi ! in the second code , you are declaring int c at scope of label JILL, so it is visible only in this label . You can not mention variable c in Label JACK ! I hope this is the problem
DOes that mean this should work
Now i am not using c anywhere ut still it doesn't work.
Pal Sudarshan
Ranch Hand
Joined: Jun 10, 2004
Posts: 52
posted
0
Hello,
I am not sure of the answer. Maybe I shouldn't be posting this, but here is what I think as of now. Maybe it will of help, but I AM NOT SURE of my own answer, okay. Just to let you know. :roll: