| Author |
unreachable code
|
Allen Bandela
Ranch Hand
Joined: Feb 16, 2006
Posts: 127
|
|
Why is it that when I write the following code, 1. do{ 2. //do something 3. }while(true); 4. //some code I get a compiler error at line 4 saying "unreachable code", whereas when I write the following code 1. int c=10; 2. do{ 3. //do something 4. }while(c==10); 5. //some code I don't get the unreachable code compile time error. At both times the condition in the while is "true".
|
Life is like a day. If the day is of no use, neither a month or a year.
|
 |
Paul Sturrock
Bartender
Joined: Apr 14, 2004
Posts: 10336
|
|
|
I could only guess that it is because c==10 is only evaluated at runtime whereas true is always true, so the compiler is able to tell you at compile time of your error.
|
JavaRanch FAQ HowToAskQuestionsOnJavaRanch
|
 |
Justin Fox
Ranch Hand
Joined: Jan 24, 2006
Posts: 802
|
|
yes, you have C initialize before the loop, so the compiler figures, maybe somewhere you'll be able to change the C to 9 or 8. but when all you have is while(true); the true can and will never be false. so the compiler recognizes and throws this error.. try to put... boolean found = true; before the loop, and then while(found); at the end of the do loop; see if it compiles then, but you'll still crash at runtime... Justin
|
You down with OOP? Yeah you know me!
|
 |
Burkhard Hassel
Ranch Hand
Joined: Aug 25, 2006
Posts: 1274
|
|
Hi Allen, yes it is a matter of compile time and runtime. The following code will not compile at the lonely semikolon at the end (representing code): and it does not compile, because the compiler knows, that between declaration /initialization of the boolean variable condi and the while condition test (// in beteween), condi cannot change since it is final. So, condi is a compile time constant and always true, and the semikolon is unreacheable code. If you delete the final keyword, the code compiles, as condi now could have changed its value // in between, which the compiler cannot know. And if you use the boolean literal true in the while condition, as in your first example, this true is also a compile time constant. You can test it by making the int-variable final in your second example. Yours, Bu.
|
 |
Allen Bandela
Ranch Hand
Joined: Feb 16, 2006
Posts: 127
|
|
|
Thanks all. So, can I safely conclude that all final variables are compile time constants? i.e they are assigned at compile time? and all non-final variables are not resolved at compile time?
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
[Allen]: So, can I safely conclude that all final variables are compile time constants? i.e they are assigned at compile time? No, being final is necessary by not sufficient to be a compile-time constant. For a variable to be a compile-time constant (a constant variable): it must be finalit must be a primitive type, or a Stringit must be initalized (at declaration) using a compile-time constant expression The rules for compile-time expressions are a bit complex - follow the link to get the precise rules. But basically it means constant variable, literal, or an expression combining other constant variables or literals. So if something "non-constant" occurs anywhere in the expression, the whole expression is not a constant. [Allen]: and all non-final variables are not resolved at compile time? That part is true.
|
"I'm not back." - Bill Harding, Twister
|
 |
Mahendra Shelke
Greenhorn
Joined: May 08, 2006
Posts: 20
|
|
boolean f = true; do { System.out.println("True"); } while (c==10); System.out.println("False"); In the above case i dont get any unreachable code error ..
|
 |
Mahendra Shelke
Greenhorn
Joined: May 08, 2006
Posts: 20
|
|
|
Sorry for the trouble guys .. got it ..
|
 |
 |
|
|
subject: unreachable code
|
|
|