• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 293
  • Last Modified:

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.
0
SLCleary
Asked:
SLCleary
5 Solutions
 
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
 
QlemoC++ DeveloperCommented:
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
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now