aspose file tools*
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
Author

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) {}
----------------------------------
Chun
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.
Bill
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
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

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

Joined: Nov 21, 2000
Posts: 40
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: while condition question