• Post Reply Bookmark Topic Watch Topic
  • New Topic

special while false  RSS feed

 
bairava surya
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

compiler termed it as unreachable statement please tell me reason when i compared with if ,do-while no error?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the loop body in the code you posted is clearly unreachable. Nothing special about it.

a do-while loop body is always executed once, since the condition is checked at the END of the run.

If conditions are given special treatment, to allow stuff like this:


I think you may even be able to use a compile=time constant. Then it is easy to flip this logging on and off with one change in one spot.

It's probably not favored any more, with various logging tools (log4j, et al), but is a holdover from how C programmers did things (That's just my guess)
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If-statements are indeed treated with a special exception. See Java Language Specification 14.21:
JLS 14.21 wrote:
This approach would be consistent with the treatment of other control structures. However, in order to allow the if statement to be used conveniently for "conditional compilation" purposes, the actual rules differ.

As an example, the following statement results in a compile-time error:


because the statement x=3; is not reachable; but the superficially similar case:


does not result in a compile-time error. An optimizing compiler may realize that the statement x=3; will never be executed and may choose to omit the code for that statement from the generated class file, but the statement x=3; is not regarded as "unreachable" in the technical sense specified here.

The rationale for this differing treatment is to allow programmers to define "flag variables" such as:


and then write code such as:


The idea is that it should be possible to change the value of DEBUG from false to true or from true to false and then compile the code correctly with no other changes to the program text.

This ability to "conditionally compile" has no relationship to binary compatibility (§13 (Binary Compatibility)). If a set of classes that use such a "flag" variable are compiled and conditional code is omitted, it does not suffice later to distribute just a new version of the class or interface that contains the definition of the flag. The classes that use the flag will not see its new value, so their behavior may be surprising, but no LinkageError will occur. A change to the value of a flag is, therefore, binary compatible with pre-existing binaries, but not behaviorally compatible.
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:It's probably not favored any more, with various logging tools (log4j, et al), but is a holdover from how C programmers did things (That's just my guess)


Although the pre-processor isn't formally part of C, I have never encountered a C compiler that didn't incorporate it. Thus, the typical idiom for conditional debugging code looks like this:

Some programmers argue in favor of the if (LOGGING_ON) alternative, on the theory that conditional compilation doesn't actually test your deliverable code (since the deliverable code doesn't include the conditionally compilable lines). However, that's still a tricky thing to manage, as a run-time flag would still have to be set at run-time, to avoid differences in code produced by an optimizing compiler. Over the years, I've seen this argument fade as optimizing compilers and virtual machines have become so sophisticated that any claims of certainty about what actually happens at the op-code/bytecode level when you run your program are increasingly suspect.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!