Evaluation order in if statement

Discussion in 'C++' started by Bogdan, Nov 24, 2011.

  1. Bogdan

    Bogdan Guest

    Hi

    I would like to know if the standard specifies the evaluation order
    of several conditions, like in the following example:

    if (is.eof() || (c == '\n'))

    Does is.eof() is evaluated first or is the other way around ?

    thanks
    Bogdan
    Bogdan, Nov 24, 2011
    #1
    1. Advertising

  2. Bogdan

    Dombo Guest

    Op 24-Nov-11 22:22, Bogdan schreef:
    > Hi
    >
    > I would like to know if the standard specifies the evaluation order
    > of several conditions, like in the following example:
    >
    > if (is.eof() || (c == '\n'))
    >
    > Does is.eof() is evaluated first or is the other way around ?


    (is.eof()) is evaluated first, and (c == '\n') will not be evaluated at
    all if (is.eof())==true.
    Dombo, Nov 24, 2011
    #2
    1. Advertising

  3. Bogdan

    Paul N Guest

    On Nov 24, 9:25 pm, Dombo <> wrote:
    > Op 24-Nov-11 22:22, Bogdan schreef:
    >
    > > Hi

    >
    > >    I would like to know if the standard specifies the evaluation order
    > > of several conditions, like in the following example:

    >
    > > if (is.eof() || (c == '\n'))

    >
    > > Does is.eof() is evaluated first or is the other way around ?

    >
    > (is.eof()) is evaluated first, and (c == '\n') will not be evaluated at
    > all if (is.eof())==true.


    Just to clarify, the operators "||", "&&" and "," are special in this
    respect - they guarantee that the first operand is evaluated first,
    and that the second is (for || and &&) only evaluated if required. For
    other operators such as "+", the operands could be evaluated in either
    order.
    Paul N, Nov 24, 2011
    #3
  4. Bogdan

    red floyd Guest

    On 11/24/2011 2:11 PM, Paul N wrote:
    > On Nov 24, 9:25 pm, Dombo<> wrote:
    >> Op 24-Nov-11 22:22, Bogdan schreef:
    >>
    >>> Hi

    >>
    >>> I would like to know if the standard specifies the evaluation order
    >>> of several conditions, like in the following example:

    >>
    >>> if (is.eof() || (c == '\n'))

    >>
    >>> Does is.eof() is evaluated first or is the other way around ?

    >>
    >> (is.eof()) is evaluated first, and (c == '\n') will not be evaluated at
    >> all if (is.eof())==true.

    >
    > Just to clarify, the operators "||", "&&" and "," are special in this
    > respect - they guarantee that the first operand is evaluated first,
    > and that the second is (for || and&&) only evaluated if required. For
    > other operators such as "+", the operands could be evaluated in either
    > order.


    Unless the operator has been overridden for a UDT, in which case it's
    equivalent to calling:

    operator&&(left, right)

    In which case evaluation order is undefined. In the OP's case, it's
    bool, so it is in fact, left to right.
    red floyd, Nov 25, 2011
    #4
  5. Bogdan

    Rainer Grimm Guest

    Rainer Grimm, Nov 26, 2011
    #5
    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. Ilias Lazaridis
    Replies:
    2
    Views:
    388
    Ilias Lazaridis
    Apr 24, 2005
  2. Replies:
    10
    Views:
    680
    Walter Roberson
    Oct 24, 2006
  3. David Côme
    Replies:
    6
    Views:
    411
    Victor Bazarov
    Mar 19, 2008
  4. Ilias Lazaridis
    Replies:
    74
    Views:
    748
    Ilias Lazaridis
    Apr 4, 2005
  5. Ilias Lazaridis
    Replies:
    18
    Views:
    330
    Bill Guindon
    Apr 9, 2005
Loading...

Share This Page