File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

while condition question

 
Chun Wang
Ranch Hand
Posts: 42
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello all.
I found that the following expression in a main() method won't compile and don't know why. can you declare a variable in the while condition? once I move it out of the condition, it works. could anyone help please? thanks
a lot!!!
---------------------------------
while(boolean b = false) {}
----------------------------------
Chun
 
bill bozeman
Ranch Hand
Posts: 1070
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are right that you can't declare a variable in a while loop. You can in a for loop, but I don't think you can in a while loop.
Bill
 
Pravin Panicker
Ranch Hand
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chun,
This would have been legal in C but not in java.
You cannot declare and assign a variable within a loop (except 'for' loop).
 
Tualha Khan
Ranch Hand
Posts: 287
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well,
But there is one thing to remember, (as far as I know ) you cannot declare and initialize a boolean variable, but you can assign one eg.
boolean b=false;
while(b=true)
{
// some code
}
Bye,
Tualha Khan
 
venu gopal
Ranch Hand
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tualha,
b=true is a boolean value. Always it's value is true. According to syntax it is correct. ( while(boolean) ) You need not remember.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic