Evaluation Order in a Condition statement

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

1. David CÃ´meGuest

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

2. ChristopherGuest

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

3. Paavo HeldeGuest

=?utf-8?Q?David_C=C3=B4me?= <> wrote in
newsp.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
4. red floydGuest

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
5. Alexander Dong Back KimGuest

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
6. Erik WikstrÃ¶mGuest

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
7. Victor BazarovGuest

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