C++ behavior for 'while' using output from function

VS 2005, C++, WinXP SP2

I'd like the experts opinions on what the 'while' loop below does?  Does it takes the result from AtPollEvent and compare against NOT being -1 and assigns that to retvalue.  OR does it assign retvalue the result from AtPollEvent and then compares against NOT being -1?

AtPollEvent(connId, eventName, (EventType *)&eventValue);
   return (int)eventValue;

int retvalue = 0;

while ((retvalue = AtPollEvent(id, Aname, (EventType *) &lockvalue) != -1))

Thanks for your input.
SLClearyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

farzanjCommented:
It runs this part
AtPollEvent(id, Aname, (EventType *) &lockvalue) != -1
If it is NOT -1 then comparison is true and while will continue.  retvalue gets the value returned by the function.
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
If you look at e.g. http://en.cppreference.com/w/cpp/language/operator_precedence, you will see that the assignment operator = has lower precedence than comparison operators == and !=. So the comparison is executed first, and the result is assigned. Whether it is intentional or not depends on the intended logic.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
philsmicronetCommented:
Thats correct but I think its always best to put brackets around the assignment. Then its clear what happens

while (  (retvalue = AtPollEvent(id, Aname, (EventType *) &lockvalue))  != -1)

So the above, the assignment is executed first. The while loop keeps on going until the value is -1.

Ofcourse this means the assignment occurs on every iteration, but at least everyone can see whats going on clearly
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

evilrixSenior Software Engineer (Avast)Commented:
Looking at this I suspect it's actually wrong. Why? Because it doesn't make much sense to have a pair of outer parenthesis. The current code looks like this...

while ((x = y != x))

I suspect (and it would make more sense) if it was written line this...

while((x = y) != x)

Note that retvalue is an int and not a bool. If AtPollEvent returns an int that would support the possibility this was just coded wrong. Unfortunately, because a bool will convert to an int (and vice versa) the compiler may very well not generate a warning for this.

Anyway, I'm not saying it is wrong just pointing out what's written there may not be what was intended (as Qlemo alluded :) )
0
sarabandeCommented:
to add to above:

a strong hint that it was wrong is that there are double parantheses both at begin and end.

also a bool expression never would convert to -1 so the condition is always true and the loop runs infinitely if there is no break or return inside

Sara
0
sarabandeCommented:
sorry, forget my second statement.

Sara
0
SLClearyAuthor Commented:
Thanks all.  Your comments helped to understand the issue ... and gives a way forward.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows XP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.