• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

while!

 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A) boolean b = false;
while(b)
System.out.println("hi");
B) while(false)
System.out.println("hi");
--> Both A) and B) has the same value false in while loop but B) doesn't compile. can u pl explain why?
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hii..
Iam not very sure..Correct me if iam wrong
In the case of (a) at compile time compiler checks
whether the expression for while(expression)is of
type boolean..hence it compiles properly..since the
resultant boolean value nothing is printed
In the case of (b) since at compile time itself
the while(expression) evaluvates to be false the
enclosing statement will never be executed,hence the
compiler cribs saying "Unreachable Statement"
Please correct me if Iam wrong
chin
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Part B doesn't compile because there is a dead code in while loop (urreachable statements produce a compile-time error). Part B will never print "hi".
Part A compiles because the value of variable my be changed.
Look at others examples:
for (;false ;)
{ System.out.println("hi"); } // won�t compile
if (false)
{System.out.println("hi"); } // will compile,
to provide the ability to conditionally compile the code.
Have a nice day,
Mariusz

[This message has been edited by Mariusz Szurnacki (edited September 13, 2001).]
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with chin.
 
Listen. That's my theme music. That's how I know I'm a super hero. That, and this tiny ad told me:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!