jQuery in Action, 3rd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes while condition question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "while condition question" Watch "while condition question" New topic

while condition question

Chun Wang
Ranch Hand

Joined: Jan 24, 2001
Posts: 42
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) {}
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
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.
Pravin Panicker
Ranch Hand

Joined: Oct 05, 2000
Posts: 62
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).

Pravin R Panicker<br />SCJP,SCWCD
Tualha Khan
Ranch Hand

Joined: Nov 22, 2000
Posts: 287
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;
// some code
Tualha Khan

SCJP2, BEA WLS 6.0, DB2 UDB 7.1
venu gopal
Ranch Hand

Joined: Nov 21, 2000
Posts: 40
b=true is a boolean value. Always it's value is true. According to syntax it is correct. ( while(boolean) ) You need not remember.
I agree. Here's the link: http://aspose.com/file-tools
subject: while condition question
It's not a secret anymore!