GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes continue and lables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "continue and lables" Watch "continue and lables" New topic
Author

continue and lables

Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
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)


By failing to prepare, you are preparing to fail.<br />Benjamin Franklin (1706 - 1790)
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

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?

Please ignore post, I have no idea what I am talking about.
Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
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

Joined: Mar 16, 2001
Posts: 986

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

Joined: Nov 02, 2000
Posts: 515
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

Joined: Mar 16, 2001
Posts: 986

The first real reason I have seen for not taking that test. (Sorry I am of no help here.)
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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.


"JavaRanch, where the deer and the Certified play" - David O'Meara
 
GeeCON Prague 2014
 
subject: continue and lables