• 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

! unreachable

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

I would have thought that while is unreachable, but it is not..
Can anybody explain?
--Cathy.
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you think the while expression is evaluated? According to the specs, it is not. This is an excerpt from the Java Tutorial:

The unlabeled form of the break statement is used to terminate the innermost switch, for, while, or do-while; the labeled form terminates an outer statement, which is identified by the label specified in the break statement.

 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
do {break;} while (true);
Hi Cathy. I don�t know why this does not cause a compiler error.
do {break;} while (f());
Or this, where f() is never executed.
But I have an idea (a guess), as a result of reading Appendix IV of the JLS amendments for assertions.

To keep the else from being associated with the second if, we enclose the if statement in {}. But a break must be contained in a switch, do, while or for statement.

[ October 12, 2003: Message edited by: Marlene Miller ]
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cathy, do you mean that you would have thought that the compiler would have complained about an unreachable statement? If so, then I think you're asking a bit too much of the compiler. The compiler mostly checks that the syntax is ok. A compiler could probably be written to check for a few weird situations such as the one in your example, but then somebody has to decide which list of weird situations to check for and which to ignore, and this checking would slow things down a bit. I think programmers mostly expect a compiler to assert the language suntax, not to assert that the programmer isn't a bonehead and doesn't write statements that don't do much of anything.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marlene, perhaps I'm missing the obvious, but why do you expect a compiler error from Cathy's example?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic