Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

While Statement

 
Abiodun Adisa
Ranch Hand
Posts: 495
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just add some parentheses to clarify the order:
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
!= 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.
 
Abiodun Adisa
Ranch Hand
Posts: 495
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic