• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

wield labeled continue statement

 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi, pls. look at the above code.
Whick statement could be inserted in the comment place and the code still be compiled cleanly?
A) label2:
B) System.out.println("Ok");
The answer is A, but why? i looked up the JLS and could find nothing.
Pls. help
James
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
James, this is a bit of a guess, but it seems to me that if you put a complete statement (such as answer B) at the insertion point, then the label1 label would apply to that statement. This would cause the continue label1 statement below to send the program execution back to a point prior to the for() statement, which would lead to an infinite loop.
I think placing a second label at that point merely assigns both label1 and label2 to the for() construct, which is compatible with the continue statement.
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is perfectly valid to have more than one label for an executable statement. Infact there are no restrictions on how many lables a statment can have!
HTH
------------------
Ajith Kallambella M.
Sun Certified Programmer for the Java�2 Platform.
IBM Certified Developer - XML and Related Technologies, V1.
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try to put the print statement and run the code. It will not work.
The interesting thing is if you use the same code with the print statement, and a break statement instead of continue it will work.
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
James,
If a continue statement with label identifier (label1 in your example) do not have a immediately enclosed statement
as while, do, or for statement than compile-time error occurs
Keeping above JLS rule in mind, In your example if you put statement (B) then it will contradict the rule mentioned above. hence option B is incorrect.
In fact option A is also incorrect. It will also give compile error by the same token
Have you tried compiling the code ? I tried using JDK1.2.2
-Sandeep Nachane
www.tipsmart.com


[This message has been edited by Sandeep Nachane (edited June 06, 2001).]
 
James Du
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all
Sandeep Nachane, I tried option A) in jdk1.2.2 with window98 platform and find it could compiled cleanly.
what's the platform you use?
I noticed the statements in JLS and so i wondered about the inconsistency.
Regards,
James
 
Scott Appleton
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can see some logic behind the different behavior between the break and continue statements.
If you break out of a loop, you're basically putting the loop and all its references behind you.
When you continue, however, you're asking the program to remember the state of the counter or control variables and begin the loop again with the next iteration. Setting the continue to a statement prior to the initialization of the loop will cause the loop to be re-entered with the counting variable(s) in an uninitialized state. Since you're maintaining a reference to those same variables in an already-initialized state, I can see where the compiler would choke.
At any rate, this is the logic I see from a high level.
 
reply
    Bookmark Topic Watch Topic
  • New Topic