This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets 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: 19649

!= 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