Evaluation order in if statement

B

Bogdan

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
 
D

Dombo

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.
 
P

Paul N

Op 24-Nov-11 22:22, Bogdan schreef:





(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.
 
R

red floyd

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top