Reputation: 8729
While loking up "dead code" thread here dead code warning in eclipse
I tried the following simple java code:
public class Test2
{
public static void main(String[] args)
{
int x = 0;
while(false)
{
x=4;
}
}
}
which correctly throws a compile time error
C:\Documents and Settings\user\Desktop\Test2.java:7: unreachable
statement
{
^ 1 error
I tweaked the code very slightly to this:
public class Test2
{
public static void main(String[] args)
{
int x =0;
while(true)
{
x=4;
}
}
}
and it compiles just fine.
Is there a reason, why this compiles fine?
logically both should cause an infiniteloop and should both cause compile time error.
Am I doing something wrong?
Upvotes: 0
Views: 6044
Reputation: 8255
Generally, a compiler will not warn you about endless loops, because they're not necessarily a programming mistake.
Also, it is mathematically impossible to give such an error message reliably for all code. It is a mathematical impossibility to write an algorithm that will prove for any program whether it will terminate, or get caught in an endless loop. Look up the "Halting problem" if you want to know the details.
Upvotes: 0
Reputation: 421230
Is there a reason, why this compiles fine?
Why shouldn't it compile fine? All statements are reachable, as opposed to the first example :-)
Many programs intentionally have infinite loops. (Think of a server for instance, that should serve clients "for ever".) It wouldn't make sense to refuse to compile infinite loops.
logically both should cause an infinite loop and should both cause compile time error.
No, only the second one should result in an infinite loop.
A while
loop goes on as long as the condition is true. Since false
is never true, the loop body will never be executed (which is why the x = 4;
is unreachable in that case).
Note that if you do...
public class Test2
{
public static void main(String[] args)
{
int x =0;
while(true)
{
x=4;
}
x = 4; <--------- unreachable!
}
}
...it won't compile, since the loop will go on for ever, and the last x = 4;
will be unreachable.
Upvotes: 5
Reputation: 53849
while(false)
never enter the loop so some code cannot be reached and that's why you got the compile time error.
while(true)
results in your example result in an infinite so you may want to have a compile time error. Nevertheless, you may have a break
in the loop exiting it and making it legit! Or you may also want actual infinite loops in some cases (for daemons for instance). That's why you have no error in this case.
Upvotes: 0
Reputation: 4945
while(false)
will never enter into this loop. so x=4;
is unreachable. while(true)
is again an infinite loop.
Upvotes: 0
Reputation: 40671
C:\Documents and Settings\user\Desktop\Test2.java:7: unreachable statement {
^ 1 error
means, there is a code which will be never executed
while(false){ /* this place will be never reached */ }
because while(condition
) expects condition
to be true
while(true){ /* this will cause infinite loop */ }
Upvotes: 0
Reputation: 425318
logically both should cause an infiniteloop
Not quite: while(false)
never enters the loop.
Also, the compiler doesn't warn about infinite loops - you may actually want one:
while(true) {
// poll some queue for work
// if work found do it
// else sleep 1 second
}
Upvotes: 8