Evaluation Order in a Condition statement

Discussion in 'C++' started by David Côme, Mar 18, 2008.

  1. Hello.

    Let A and B be 2 boolean expressions.
    In the instruction if(A && B){/*..*/}, does the standard certify that A
    will be evaluated first ?

    Thanks a lot.
     
    David Côme, Mar 18, 2008
    #1
    1. Advertising

  2. David Côme

    Christopher Guest

    On Mar 18, 3:32 pm, David Côme <> wrote:
    > Hello.
    >
    > Let A and B be 2 boolean expressions.
    > In the instruction if(A && B){/*..*/}, does the standard certify that A
    > will be evaluated first ?
    >
    > Thanks a lot.


    left to right
     
    Christopher, Mar 18, 2008
    #2
    1. Advertising

  3. David Côme

    Paavo Helde Guest

    =?utf-8?Q?David_C=C3=B4me?= <> wrote in
    news:eek:p.t78bb5kyrttu86@debian:

    > Hello.
    >
    > Let A and B be 2 boolean expressions.
    > In the instruction if(A && B){/*..*/}, does the standard certify that A
    > will be evaluated first ?


    Even more: if A yields false, B is not evaluated at all.
     
    Paavo Helde, Mar 18, 2008
    #3
  4. David Côme

    red floyd Guest

    David Côme wrote:
    > Hello.
    >
    > Let A and B be 2 boolean expressions.
    > In the instruction if(A && B){/*..*/}, does the standard certify that A
    > will be evaluated first ?
    >


    I believe it depends upon whether operator&& is user-defined.
    For built-in &&, yes, A is evaluated first, and B will not be evaluated
    at all, if A is true.

    For user defined &&, I believe the order is unspecified, and that both A
    and B will be evaluated.
     
    red floyd, Mar 18, 2008
    #4
  5. On Mar 19, 7:40 am, Christopher <> wrote:
    > On Mar 18, 3:32 pm, David Côme <> wrote:
    >
    > > Hello.

    >
    > > Let A and B be 2 boolean expressions.
    > > In the instruction if(A && B){/*..*/}, does the standard certify that A
    > > will be evaluated first ?

    >
    > > Thanks a lot.

    >
    > left to right


    This is absolutely right! Lazy evaluation!

    Cheers,
     
    Alexander Dong Back Kim, Mar 19, 2008
    #5
  6. On 2008-03-19 05:40, Alexander Dong Back Kim wrote:
    > On Mar 19, 7:40 am, Christopher <> wrote:
    >> On Mar 18, 3:32 pm, David Côme <> wrote:
    >>
    >> > Hello.

    >>
    >> > Let A and B be 2 boolean expressions.
    >> > In the instruction if(A && B){/*..*/}, does the standard certify that A
    >> > will be evaluated first ?

    >>
    >> > Thanks a lot.

    >>
    >> left to right

    >
    > This is absolutely right! Lazy evaluation!


    Short circuit evaluation, lazy evaluation is something else.

    --
    Erik Wikström
     
    Erik Wikström, Mar 19, 2008
    #6
  7. Erik Wikström wrote:
    > On 2008-03-19 05:40, Alexander Dong Back Kim wrote:
    >> On Mar 19, 7:40 am, Christopher <> wrote:
    >>> On Mar 18, 3:32 pm, David Côme <> wrote:
    >>>
    >>>> Hello.
    >>>
    >>>> Let A and B be 2 boolean expressions.
    >>>> In the instruction if(A && B){/*..*/}, does the standard certify
    >>>> that A will be evaluated first ?
    >>>
    >>>> Thanks a lot.
    >>>
    >>> left to right

    >>
    >> This is absolutely right! Lazy evaluation!

    >
    > Short circuit evaluation, lazy evaluation is something else.


    And actually in 'A && B' the expression 'B' is *not* evaluated at all
    if 'A' yields 'false'.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Mar 19, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -
    Replies:
    12
    Views:
    698
    Remon van Vliet
    Jun 15, 2005
  2. Shill

    For loop condition evaluation

    Shill, Aug 5, 2003, in forum: C Programming
    Replies:
    6
    Views:
    896
  3. Replies:
    10
    Views:
    686
    Walter Roberson
    Oct 24, 2006
  4. Bogdan
    Replies:
    4
    Views:
    748
    Rainer Grimm
    Nov 26, 2011
  5. Bill W.
    Replies:
    13
    Views:
    299
    Phillip Gawlowski
    May 9, 2011
Loading...

Share This Page