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

Posted on 2011-10-25
Last Modified: 2012-05-12
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.
Question by:SLCleary
    LVL 31

    Assisted Solution

    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.
    LVL 67

    Accepted Solution

    If you look at e.g., 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.
    LVL 1

    Assisted Solution

    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
    LVL 39

    Assisted Solution

    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 :) )
    LVL 32

    Assisted Solution

    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

    LVL 32

    Expert Comment

    sorry, forget my second statement.


    Author Closing Comment

    Thanks all.  Your comments helped to understand the issue ... and gives a way forward.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    If you have done a reformat of your hard drive and proceeded to do a successful Windows XP installation, you may notice that a choice between two operating systems when you start up the machine. Here is how to get rid of this: Click Start Clic…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now