Velocity Reviews > Precedence Misunderstanding

# Precedence Misunderstanding

Stefan Ram
Guest
Posts: n/a

 07-17-2012
"Charles Richmond" <(E-Mail Removed)> writes:
>For example, if you have a long series of logical AND's and OR's like this:
>a && b || c || d && x && y || z

I answered immediately and precisely to this point
in my first contribution to this thread

<(E-Mail Removed)-berlin.de>

, but my answer is just being ignored.

(The OP thinks that in this case the standard prescribed

(((((( a && b )|| c )|| d )&& x )&& y )|| z )

, when it says that »that a series of "logical and"
and "logical or" operations will occur left to right«.
I immediately understood this and answered it with the

Ben Pfaff
Guest
Posts: n/a

 07-17-2012
"Charles Richmond" <(E-Mail Removed)> writes:

> "Ben Pfaff" <(E-Mail Removed)> wrote in message
> For example, if you have a long series of logical AND's and OR's like this:
>
> a && b || c || d && x && y || z
>
> How will "&&" having a higher precedence than "||" effect this at
> all??? ISTM that evaluation would be the *same* if "&&" and "||" were
> of equal precedence.

Precedence doesn't affect the order of evaluation of the
terminals in this case but it affects the result. Your
expression is equivalent to:
(a && b) || c || (d && x && y) || z
which could have a different value from:
a && (b || c || d) && x && (y || z)
even though in each case the terminals are always evaluated
left-to-right (a fact that I hadn't noticed before) if they are
evaluated at all.

Ben Bacarisse
Guest
Posts: n/a

 07-17-2012
"Charles Richmond" <(E-Mail Removed)> writes:
<snip>
> Can someone explain how "&&" can have a *higher* precedence than
> "||"... when any series of AND's and OR's are just going to be
> done left to right???

No, because it's not true. A sequence of &&s is done left to right, and
a sequence of ||s is done left to right, but when &&s and ||s are mixed
the precedence matters.

> For example, if you have a long series of logical AND's and OR's like this:
>
> a && b || c || d && x && y || z
>
> How will "&&" having a higher precedence than "||" effect this at
> all??? ISTM that evaluation would be the *same* if "&&" and "||" were
> of equal precedence.
> Create any series of AND's and OR's you want, and please show me how
> the precedence of "&&'" and "||" will make any difference under the
> current C standard.

Lets take a simple example

a || b && c

means

a || (b && c)

but if && and || had equal precedence it would mean

(a || b) && c

These two expressions are not the same -- the precedence makes a
difference.

> Also ISTM that in the above expression, it will *not* matter if one
> adds parentheses around any operator/operand pair or grouping. The
> result seems to be the same. Please show me where I'm wrong.

It's a messy example but

a && b || c || d && x && y || z

means

((((a && b) || c) || ((d && x) && y))) || z

so it is true if z is non-zero regardless of the values of the other

a && (b || c || d) && x && (y || z)

and your get quite another expression. For example, a, x and one of b,
c or d must also be non-zero for it to be true.

<snip>
--
Ben.

Ben Pfaff
Guest
Posts: n/a

 07-17-2012
Ben Bacarisse <(E-Mail Removed)> writes:

> "Charles Richmond" <(E-Mail Removed)> writes:
> <snip>
>> Can someone explain how "&&" can have a *higher* precedence than
>> "||"... when any series of AND's and OR's are just going to be
>> done left to right???

>
> No, because it's not true. A sequence of &&s is done left to right, and
> a sequence of ||s is done left to right, but when &&s and ||s are mixed
> the precedence matters.

[...]

> Lets take a simple example
>
> a || b && c
>
> means
>
> a || (b && c)
>
> but if && and || had equal precedence it would mean
>
> (a || b) && c
>
> These two expressions are not the same -- the precedence makes a
> difference.

I think Charles is saying that, regardless of precedence, a is
evaluated before b and b is evaluated before c. That's correct,
I think.

BartC
Guest
Posts: n/a

 07-17-2012
"Charles Richmond" <(E-Mail Removed)> wrote in message
news:ju4ba5\$lso\$(E-Mail Removed)...

> The crux of what I do *not* understand is this:
>
> Can someone explain how "&&" can have a *higher* precedence than
> "||"... when any series of AND's and OR's are just going to be
> done left to right???
>
> For example, if you have a long series of logical AND's and OR's like
> this:
>
> a && b || c || d && x && y || z
>
> How will "&&" having a higher precedence than "||" effect this at all???
> ISTM that evaluation would be the *same* if "&&" and "||" were of equal
> precedence.
>
> Create any series of AND's and OR's you want, and please show me how the
> precedence of "&&'" and "||" will make any difference under the current C
> standard.

( a && b || c || d && x && y || z)

might be written as follows, with parentheses added to show default
structure**. The indices I've added are arbitrary:

((a and1 b) or2 c or3 ((d and4 x) and5 y) or6 z)

Or it can be written lisp-style like this:

(or6 (or3 (or2 (and1 a b) c) (and5 (and4 d x) y)) z)

You can see that evaluation would need to be 'bottom-up', rather than
left-to-right, although they might sometimes correspond.

> Also ISTM that in the above expression, it will *not* matter if one adds
> parentheses around any operator/operand pair or grouping. The result
> seems to be the same. Please show me where I'm wrong.

Because of short-circuit evaluation, and because the result of such an
expression is only ever going to be 0 or 1, it can be difficult to
appreciate differences between one such logical expression and another.

(** I won't use the terms evaluation order, precedence, or associativity
which are full of pit-falls.)

--
Bartc

Ben Bacarisse
Guest
Posts: n/a

 07-17-2012
Ben Bacarisse <(E-Mail Removed)> writes:

> "Charles Richmond" <(E-Mail Removed)> writes:
> <snip>
>> Can someone explain how "&&" can have a *higher* precedence than
>> "||"... when any series of AND's and OR's are just going to be
>> done left to right???

>
> No, because it's not true. A sequence of &&s is done left to right, and
> a sequence of ||s is done left to right, but when &&s and ||s are mixed
> the precedence matters.

....but this could be expressed more clearly: the precedence matters, but
an operand that is further to the right is never evaluated before one to
its left.

This, by the way, is specific to the short-circuit nature of && and ||,
and of the fact that the left-hand operand is always evaluated first.
The right-hand operand may not be evaluated at all, so the notion of a
strict left-to-right evaluation is not exactly true -- the evaluation
can skip one or more operands.

<snip>
--
Ben.

Ben Bacarisse
Guest
Posts: n/a

 07-17-2012
Ben Pfaff <(E-Mail Removed)> writes:

> Ben Bacarisse <(E-Mail Removed)> writes:
>
>> "Charles Richmond" <(E-Mail Removed)> writes:
>> <snip>
>>> Can someone explain how "&&" can have a *higher* precedence than
>>> "||"... when any series of AND's and OR's are just going to be
>>> done left to right???

>>
>> No, because it's not true. A sequence of &&s is done left to right, and
>> a sequence of ||s is done left to right, but when &&s and ||s are mixed
>> the precedence matters.

>
> [...]
>
>> Lets take a simple example
>>
>> a || b && c
>>
>> means
>>
>> a || (b && c)
>>
>> but if && and || had equal precedence it would mean
>>
>> (a || b) && c
>>
>> These two expressions are not the same -- the precedence makes a
>> difference.

>
> I think Charles is saying that, regardless of precedence, a is
> evaluated before b and b is evaluated before c. That's correct,
> I think.

Yes, it is, but he seems to think that that means either that precedence
has no effect, or that && and || must have the same precedence.

My answer should really have unpicked these three things: yes you get
left-to-right evaluation (though it can stop short, of course); yes &&
does have higher precedence than || (simply by definition of the
language); and yes the precedence makes a difference to the expression.

--
Ben.

Willem
Guest
Posts: n/a

 07-17-2012
Ben Pfaff wrote:
) "Charles Richmond" <(E-Mail Removed)> writes:
)
)> "Ben Pfaff" <(E-Mail Removed)> wrote in message
)> For example, if you have a long series of logical AND's and OR's like this:
)>
)> a && b || c || d && x && y || z
)>
)> How will "&&" having a higher precedence than "||" effect this at
)> all??? ISTM that evaluation would be the *same* if "&&" and "||" were
)> of equal precedence.
)
) Precedence doesn't affect the order of evaluation of the
) terminals in this case but it affects the result. Your
) expression is equivalent to:
) (a && b) || c || (d && x && y) || z
) which could have a different value from:
) a && (b || c || d) && x && (y || z)
) even though in each case the terminals are always evaluated
) left-to-right (a fact that I hadn't noticed before) if they are
) evaluated at all.

Yes. But some terminals could be skipped because of short-circuiting.
And different ones could be skipped if the parentheses were different.

SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT

Willem
Guest
Posts: n/a

 07-17-2012
Charles Richmond wrote:
) The crux of what I do *not* understand is this:
)
) Can someone explain how "&&" can have a *higher* precedence than
) "||"... when any series of AND's and OR's are just going to be
) done left to right???

The precedence determines how the results *will be used*.
It does not determine the order in which they are evaluated.

) For example, if you have a long series of logical AND's and OR's like this:
)
) a && b || c || d && x && y || z
)
) How will "&&" having a higher precedence than "||" effect this at all???

This is, step by step, what the program will do:

/* Construct for the expression a && b || c || d && x && y || z */
int abcdxyz(int a, int b, int c, int d, int x, int y, int z)
{
if (a) {
if (b) {
return 1;
}
}
if (c) {
return 1;
}
if (d) {
if (x) {
if (y) {
return 1;
}
}
}
if (z) {
return 1;
}
return 0;
}

Now, as you can see: a, b, c, d, x, y, z are evaluated in that order.

Suppose that '&&' had higher precedence, it would become the following:

/* Construct for the expression a && b || c || d && x && y || z
* with precedences reversed */
int abcdxyz(int a, int b, int c, int d, int x, int y, int z)
{
if (!a) {
return 0;
}
if (!b) {
if (!c) {
if (!d) {
return 0;
}
}
}
if (!x) {
return 0;
}
if (!y) {
if (!z) {
return 0;
}
}
return 1;
}

As you can see, they are still evaluated in the same order, yet still they
are grouped differently.

) Create any series of AND's and OR's you want, and please show me how the
) precedence of "&&'" and "||" will make any difference under the current C
) standard.

See above.

) I can *not* understand how "&&" can have a higher precedence than "||"...
) when it seems to make *no* difference in *any* expression I've seen.

int a() { printf("a=0\n"); return 0; }
int b() { printf("b=1\n"); return 1; }
int c() { printf("c=1\n"); return 1; }
int main() {
printf("First\n");
printf("Result: %d\n", (a() && b()) || c());
printf("Second\n");
printf("Result: %d\n", a() && (b() || c()));
return 0;
}

There you go.

SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT

Rui Maciel
Guest
Posts: n/a

 07-17-2012
Ben Pfaff wrote:

> I think there's a lot of confusion on this point because we
> side effects don't exist in math, so order of evaluation doesn't
> matter. Precedence does, but that's a different issue.

<snip/>

This is nonsense. What you refer as being "side effects" is nothing more
than the result of ignoring an entire subspace of an operator's domain, and
how the components of the subspace you chose to ignore can also influence
how the remaining components are mapped.

Rui Maciel