CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Sep 2003
    Posts
    815

    do while loops with &&

    Hi,

    Can I do something like this:

    X *pX;

    do
    {
    //.....
    }while((pX != NULL) && (pX->func() >2))

    I mean can I be sure that the left condition pX!= NULL, will be checked before the right condition (pX->func() >2), hence if the right condition will be checked only if the left condition is true?

    Thanks
    Avi123

  2. #2
    Join Date
    Aug 1999
    Location
    <Classified>
    Posts
    6,882

    Re: do while loops with &&

    Yes, if pX is checked for NULL in brackets first then it will be executed first, being an AND openration both conditions will be checked always.

    But if you want to make it sure and remove compiler dependancies then you shold have do those check before using while as follows,
    Code:
    X *pX;
    
    do
    {
    //.....
    
    bool bX = (pX != NULL) ;
    bX = bX && (pX->func() >2) ;
    
    }while(bX)
    Regards,
    Ramkrishna Pawar

  3. #3
    Join Date
    Apr 2007
    Posts
    21

    Re: do while loops with &&

    Code:
    X *pX;
    First of all you need to allocate memory to pX ..

    And the code you wrote is best option to check for NULL first.

    regards

  4. #4
    Join Date
    Sep 2003
    Posts
    815

    Re: do while loops with &&

    Thank you both for your quick response
    However I still not sure,

    Krishnaa: you say that the left side will be calulated first, however then you say that in order not to depend on compiler, are there any compilers that may calculate the right side before the left side

    irs_b: when you say best option, is this the only option or is there might be compiler that will check the right side first?

    Thanks again
    Avi123

  5. #5
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: do while loops with &&

    When using && ...

    Left-to-right evaluation is guaranteed and the second operand is
    not evaluated if the first operand is false.

  6. #6
    Join Date
    Sep 2003
    Posts
    815

    Re: do while loops with &&

    Thanks Philip,

    Are there any other operators where left to right order is not guaranteed?

    Thanks
    Avi123

  7. #7
    Join Date
    Apr 2007
    Posts
    21

    Re: do while loops with &&

    You can get the associativity of all operators below link

    C++ operator precedence

  8. #8
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: do while loops with &&

    Quote Originally Posted by Philip Nicoletti
    When using && ...

    Left-to-right evaluation is guaranteed and the second operand is
    not evaluated if the first operand is false.
    Just to emphasize the point, this is guaranteed by the language, and is not compiler-dependent. Every compiler out there will conform on this point.

    Mike

  9. #9
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: do while loops with &&

    Actually the built in "&&" and "||" operators will short circuit.

    For &&, if the first is falst the second is not evaluated.
    For ||, if the first ie true, the second is not evaluated.

    However if you overload the operator then both are evaluated under all conditions (hence the recommendation to never overload these operators!)
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  10. #10
    Join Date
    Apr 2007
    Posts
    43

    Re: do while loops with &&

    That type of check is extremely common in production code.

    I've also seen it frequently abbreviated as:

    Code:
    Thing *ptr;
    if(ptr && ptr->foo())
    ...
    though some might argue that the form 'if(ptr)' is a poor coding standard.

  11. #11
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: do while loops with &&

    And as soon as someone writes

    Code:
    template<class RHS_TYPE>
    bool Thing::operator &&(Thing const &lhs,  RHS_TYPEconst&rhs);
    It Immediately Breaks!
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  12. #12
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: do while loops with &&

    But as your pointed out, it is not recommended to overload operator&& anyway....

  13. #13
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: do while loops with &&

    And I also tend to be paranoid...

    Even if I don't overload, how can I guarantee 100% that some vendor will not change their implementation at some point and implement the overload.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured