• 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

while(false)

 
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
this code compiles and does not give error unreachable statement.
Why?
Can anyone please explain

public class ReachTest{
public static void main(String[] args){
boolean h = false;
while(h){
System.out.println("Hi");
h = true; // code compiles with or without this line.
}
}
}
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some answers here:
http://www.javaranch.com/ubb/Forum24/HTML/012797.html
http://www.javaranch.com/ubb/Forum24/HTML/008990.html
http://www.javaranch.com/ubb/Forum24/HTML/009732.html
http://www.javaranch.com/ubb/Forum24/HTML/010179.html
http://www.javaranch.com/ubb/Forum24/HTML/012859.html
...
And more if you try to search for "while false" using the search engine...
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
[This message has been edited by Valentin Crettaz (edited December 14, 2001).]
 
Ranch Hand
Posts: 1246
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try putting..
final boolean h = false;
 
Neha Sawant
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Valentin,
i have read all the forums but still fail to understand when it should compile and when not.
FEING,
I made it final and it is not compiling saying value of h cannot be changed.
I thought since it is not entering the loop it should not give this error.
All this should not come hopefully in my exams.
I have my exams tommorrow.
Bye
neha
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read this then:
http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#236365
And good luck for your exam !
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Neha Sawant
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Valentin,
i have made a conclusion.
Pleaase tell me i am right
while(false){//some satemant} will not work since false is a constant.
whereas
if it is not a constant but an expression whose result is false will work
say
int x=1;
while(x>2){
//somecode }
this will compile
while(false){x=3;} compiles.
please help
neha
 
Neha Sawant
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry my last eg i meant to say wont compile
neha
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
right you may have a variable statement being false as a condition to the while loop and it will work. However, if you write "false" or you give a constant (final variable) being false then it won't compile because the statement inside the while loop is unreachable and the compiler can figure that out during compilation.
See, you can change the value of a variable at runtime and that's why the compiler cannot complain about a false condition. However if you have a constant boolean expression whose value is false, the compiler knows that there is no way to change that value at runtime and that the while loop will never be executed, hence the unreachable statement. Try to think as the compiler does... You'll see there is logic in there
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Neha Sawant
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks val,
but when i put final boolean h=false;
while(h){//some code)
then why does this compile.
since the compiler should know that h is final and cannot be changed
neha
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
on my machine it doesn't compile

and says

What about your code ?

------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Neha Sawant
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it's compiling on mine.
class reactest{
public static void main(String args[]){
final boolean h=false;
int x=3;
while(h){
x=3;
}
}}
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
weird because on mine (jdk1.4 b2 on Win2K) it says

then something must be wrong on your side because such code cannot compile according to the JLS

------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Neha Sawant
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
fine val i will keep that in mind
neha
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just my two cents.
if doesn't behave as while to this respect. "if(false) ...." will compile ok. This is so to allow conditional compilation of the sentences after if based on the variable tested.
 
Neha Sawant
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jose.
i am aware of that
neha
 
reply
    Bookmark Topic Watch Topic
  • New Topic