File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes While Statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "While Statement" Watch "While Statement" New topic

While Statement

Abiodun Adisa
Ranch Hand

Joined: Jan 17, 2002
Posts: 495
i want to obtain a value and assign it to b in a while statement and i also want to test to see if B is null in the same statement, Please how do i do this

I tried but it resulted in errors

Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Just add some parentheses to clarify the order:

"I'm not back." - Bill Harding, Twister
Rob Spoor

Joined: Oct 27, 2005
Posts: 19655

!= has precedence over =, so if you write it without parentheses the compiler will see this:

Now seeing as b is not a boolean, this will fail.

If you add parentheses as Jim has done, the compiler will have no problems anymore; it will first use the assignment to b, and only then compare the value of b to null.

How To Ask Questions How To Answer Questions
Abiodun Adisa
Ranch Hand

Joined: Jan 17, 2002
Posts: 495
I agree. Here's the link:
subject: While Statement
Similar Threads
Checking variable initialization...
Resource closing in JDBC
unreachable statement
if and while statements
Trouble Displaying all the data