Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Evaluation order in if statement

Reply
Thread Tools

Evaluation order in if statement

 
 
Bogdan
Guest
Posts: n/a
 
      11-24-2011
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
 
Reply With Quote
 
 
 
 
Dombo
Guest
Posts: n/a
 
      11-24-2011
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.
 
Reply With Quote
 
 
 
 
Paul N
Guest
Posts: n/a
 
      11-24-2011
On Nov 24, 9:25*pm, Dombo <do...@disposable.invalid> 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.
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      11-25-2011
On 11/24/2011 2:11 PM, Paul N wrote:
> On Nov 24, 9:25 pm, Dombo<do...@disposable.invalid> 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.

 
Reply With Quote
 
Rainer Grimm
Guest
Posts: n/a
 
      11-26-2011
Hi,
that's a special form of lazy evaluation, called short-circuit evaluation. Have a look here http://en.wikipedia.org/wiki/Short-circuit_evaluation.

Greetings from Rottenburg,
Rainer
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Evaluation Order in a Condition statement David Côme C++ 6 03-19-2008 05:25 PM
Evaluation order of assignment statement nachch@gmail.com C Programming 10 10-24-2006 12:16 AM
[EVALUATION] - E03 - jamLang Evaluation Case Applied to Python Ilias Lazaridis Python 2 04-24-2005 05:29 PM
[EVALUATION] - E04 - jamPersist Evaluation Case Applied to Ruby Ilias Lazaridis Ruby 18 04-09-2005 04:45 PM
[EVALUATION] - E03 - jamLang Evaluation Case Applied to Ruby Ilias Lazaridis Ruby 74 04-04-2005 05:29 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57