Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

continue and lables

 
Dale DeMott
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why will this NOT compile? The system says it doesn't know what the lable goHere is.
goHere:System.out.println("START");
for (int i = 0; i < 10; i++) {
if (i == 5)
continue goHere;
}
done : System.out.println("DONE");
BUT if I do the following... it will compile...
System.out.println("START");
goHere : for (int i = 0; i < 10; i++) {
if (i == 5)
continue goHere;
}
done : System.out.println("DONE");
What's up? Why doesn't it know what goHere is in the first scenario?
-Dale
------------------
By failing to prepare, you are preparing to fail.
Benjamin Franklin (1706 - 1790)
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure what the compile problem is.
But can I ask you why you are using gotos and labels? The code you show as an example just loops forever, not that there is any problem with that. But usually there are better ways of writing the code if you are using gotos and labels.
Not sure why java even included gotos and labels, they were smart enough to get rid of pointers, when not gotos?
 
Dale DeMott
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I fully agree w/you. And to tell you the truth, it does NOT go forever. In fact the code does NOT reset the value to 0, which in itself is odd.
System.out.println("START");
goHere : for (int i = 0; i < 10; i++)
{
if (i == 5)
continue goHere;
}
done : System.out.println("DONE");
Tell me what you think.
Dale

------------------
By failing to prepare, you are preparing to fail.
Benjamin Franklin (1706 - 1790)
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I think that if both agree that gotos and labels are not good to use I think you should rewrite the code.
As to why the code does strange things when you are using gotos and labels I have no idea.
 
Dale DeMott
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only reason I'm reviewing this is for the Certification test. Other than that, I would never code like this. There are some things that my certification book mentions that I wanted to make sure I understood. This was one of them.
Dale

------------------
By failing to prepare, you are preparing to fail.
Benjamin Franklin (1706 - 1790)
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first real reason I have seen for not taking that test. (Sorry I am of no help here.)
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the first example you have placed the label on a statement which is out of scope for the continue statement, so it can't find it. Continue is NOT the same as GOTO, it can only go within the statement construct that it is part of. There are no non-local jumps.
When you put the label on the for loop, the continue IN the for loop can now get at it.
From the JLS 14.7 Labeled Statements
The scope of a label declared by a labeled statement is the statement immediately enclosed by the labeled statement.

and
14.15 The continue Statement

A continue statement with label Identifier attempts to transfer control to the enclosing labeled statement (�14.7) that has the same Identifier as its label; that statement, which is called the continue target, then immediately ends the current iteration and begins a new one. The continue target must be a while, do, or for statement or a compile-time error occurs. A continue statement must refer to a label within the immediately enclosing method or initializer block. There are no non-local jumps.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic