Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Labels

 
K Anshul
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following code works


but the follwoing doesn't


Please tell me the reason for this.
 
Victor Santos
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

I think JILL: int c = 0 is not a statement.
Here is what I found online about statements in Java.
Click on the link below.
http://www.sniffer.net/bookshelf_do_sniffer/java/langref/ch06_js.htm
 
vydhehi paruchuri
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to JLS these are the statements:

Statement:
StatementWithoutTrailingSubstatement
LabeledStatement
IfThenStatement
IfThenElseStatement
WhileStatement
ForStatement

StatementWithoutTrailingSubstatement:
Block
EmptyStatement
ExpressionStatement
SwitchStatement
DoStatement
BreakStatement
ContinueStatement
ReturnStatement
SynchronizedStatement
ThrowStatement
TryStatement

StatementNoShortIf:
StatementWithoutTrailingSubstatement
LabeledStatementNoShortIf
IfThenElseStatementNoShortIf
WhileStatementNoShortIf
ForStatementNoShortIf

I think, Assignment statement is not pure statement.

You can compile and run the program by changing the label to

JILL:
{
int p = 0;
}
Thanks,

Vydhehi
 
siva prakash
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

void func() {

JACK : {
int i = 10 ;
}
}


works , but not

void func() {

JACK : int i = 10 ;
}


bye
siva
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic