473,386 Members | 1,835 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

&& and || -- precedence evaluation doubt

According to the precedence rules && has a higher precedence than
|| and both are 'left to right' associative. So in an expression
like

++i || ++j && ++k

where i, j, k are 0, -1, 0 (before the expression is evaluated),
shouldn't the && part be evaluated before the || part? In which
case will the evaluation be stopped short if the right side
evaluates to 1 (true)?

Or is the evaluation of this expression implementation dependent?

--
Roshan Mathews
Dr. MGR Deemed University
Chennai

Apr 18 '06 #1
8 1793


Roshan Mathews wrote On 04/18/06 11:54,:
According to the precedence rules && has a higher precedence than
|| and both are 'left to right' associative. So in an expression
like

++i || ++j && ++k

where i, j, k are 0, -1, 0 (before the expression is evaluated),
shouldn't the && part be evaluated before the || part? In which
case will the evaluation be stopped short if the right side
evaluates to 1 (true)?
Precedence is not evaluation order. (Don't feel bad;
many others have confused the two.) Precedence says that
the expression above means the same as

++i || (++j && ++k) /* RIGHT */

rather than

(++i || ++j) && ++k /* RWONG */

Once the meaning is determined (by precedence or by
explicit grouping or by a combination), the evaluation
proceeds. For this expression, ++i is evaluated first.
Since it turns out to be non-zero (for the given values),
the rest of the expression is not evaluated at all, and
the result of the expression is 1.
Or is the evaluation of this expression implementation dependent?


No.

--
Er*********@sun.com

Apr 18 '06 #2
"Roshan Mathews" <rm******@gmail.com> wrote:
According to the precedence rules && has a higher precedence than
|| and both are 'left to right' associative. So in an expression
like

++i || ++j && ++k

where i, j, k are 0, -1, 0 (before the expression is evaluated),
shouldn't the && part be evaluated before the || part?


No. This higher precendence means nothing more than that a || b && c is
equal to a || (b && c), not to (a || b) && c. It says nothing about the
order in which any of the operands are evaluated.
This is just as true of * and +, or of [] and ==. [] having higher
precedence than == means that x == y[z] does evaluate to x == (y[z]),
not to the (nonsensical) (x == y)[z]. It does not mean that y and z are
evaluated before x.

There is a special case, however, for the && and || operators, which
guarantees the opposite of your what you suppose. They are said to
short-circuit; that is, they are required to be evaluated left-to-right,
and when the value of the entire expression is known, that value is
returned and no other operands are even looked at.
This means that ++i || ++j && ++k is handled like this:

Evaluate ++i. If this is true (non-zero), the whole expression is true
(to be precise, it is 1); stop here.
Ok, we now know that i used to be -1, and ++i was 0 (and since both
|| and && also introduce a sequence point, so is i, now; this is not
important in this example, but can be in other circumstances; e.g.,
it means that --i && a[i] does not invoke undefined behaviour).
Evaluate ++j. If it is zero, the sub-expression ++j && ++k is zero,
and since ++i is also zero if we've got this far, so is the entire
expression; if so, stop here.
Now we know that ++i was 0 and ++j was non-zero.
Evaluate ++k. If it's false, the expression is false (0); if it's
true (non-zero), the expression is true (specifically 1).

Richard
Apr 18 '06 #3
Roshan Mathews wrote:

According to the precedence rules && has a higher precedence than
|| and both are 'left to right' associative. So in an expression
like

++i || ++j && ++k

where i, j, k are 0, -1, 0 (before the expression is evaluated),
shouldn't the && part be evaluated before the || part? In which
case will the evaluation be stopped short if the right side
evaluates to 1 (true)?

Or is the evaluation of this expression implementation dependent?


Just because && has higher precedence than "||" doesn't mean that it
must be executed first, merely that the entire right-side must be
evaluated before or-ing it in. The entire boolean sequence still
needs to be evaluated left-to-right.

Your statement is equivalent to:

++i || ( ++j && ++k )

The "++i" is evaluated first. Because "++i" is non-zero, the rest of
the expression is not evaluated.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Apr 18 '06 #4
Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
There is a special case, however, for the && and || operators, which
guarantees the opposite of your what you suppose. They are said to
short-circuit; that is, they are required to be evaluated left-to-right,


Boring pedantic question: Are they required to really be evaluated
left-to-right, or must the implementation merely behave "as-if" the
arguments had been evaluated left-to-right?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Apr 19 '06 #5
In article <e2**********@chessie.cirr.com> Christopher Benson-Manica <at***@norge.freeshell.org> writes:
Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
There is a special case, however, for the && and || operators, which
guarantees the opposite of your what you suppose. They are said to
short-circuit; that is, they are required to be evaluated left-to-right,


Boring pedantic question: Are they required to really be evaluated
left-to-right, or must the implementation merely behave "as-if" the
arguments had been evaluated left-to-right?


Always as-if.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Apr 19 '06 #6
Christopher Benson-Manica wrote:
Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
There is a special case, however, for the && and || operators, which
guarantees the opposite of your what you suppose. They are said to
short-circuit; that is, they are required to be evaluated left-to-right,


Boring pedantic question: Are they required to really be evaluated
left-to-right, or must the implementation merely behave "as-if" the
arguments had been evaluated left-to-right?


There is no 'merely' about it for a conforming implementation. :-)

An implementation can replace...

i || j++ && 0;

....with...

if (!i) j++;

However...

i || 0 && j++;

....must be a no-op (assuming i is not volatile.)

the more classic case is something like...

if (p && *p)

Search for Chris Torek's posts on how an implementation can
'silently' trap by dereferencing a potential null pointer in parallel
to the null test, but it must 'cope' with that trapping and behave
as if it worked correctly.

There are circumstances involving volatile objects where a practical
conforming implementation must still be very careful about how it
handles the evaluation order of certain expressions.

--
Peter

Apr 19 '06 #7
Christopher Benson-Manica <at***@norge.freeshell.org> wrote:
Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
There is a special case, however, for the && and || operators, which
guarantees the opposite of your what you suppose. They are said to
short-circuit; that is, they are required to be evaluated left-to-right,


Boring pedantic question: Are they required to really be evaluated
left-to-right, or must the implementation merely behave "as-if" the
arguments had been evaluated left-to-right?


Well, of course, everything is as-if. However, that as-if had also
better involve not reading any volatiles, not calling rand(), not
causing any FP exceptions, and so on, and so forth. Your program really
must not be able to tell the difference in any reliable way. (Difference
in execution time, for example, does not count as reliable.)

Richard
Apr 19 '06 #8
On Wed, 19 Apr 2006 00:08:08 +0000 (UTC), Christopher Benson-Manica
<at***@norge.freeshell.org> wrote:
Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
There is a special case, however, for the && and || operators, which
guarantees the opposite of your what you suppose. They are said to
short-circuit; that is, they are required to be evaluated left-to-right,


Boring pedantic question: Are they required to really be evaluated
left-to-right, or must the implementation merely behave "as-if" the
arguments had been evaluated left-to-right?

It's required that you not know or care ;-)

--
Al Balmer
Sun City, AZ
Apr 19 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Vivek N | last post by:
Hi Folks, This question may well have been asked and answered before. But, sorry that I couldn't find one from the archives. I typed up this program and compiled it with gcc 3.3.2 main() { int...
21
by: siliconwafer | last post by:
Hi, In case of following expression: c = a && --b; if a is 0,b is not evaluated and c directly becomes 0. Does this mean that && operator is given a higher precedence over '--'operator? as...
17
by: orekinbck | last post by:
Hi There Say I want to check if object1.Property1 is equal to a value, but object1 could be null. At the moment I have code like this: if (object1 != null) { if (object1.Property ==...
9
by: marko | last post by:
/* code start */ int a = 0; /* expected evaluation and excution order with precedence in mind /* False(3) , True(1), False(2) */ if ( (a=1) == 0 || 0 != 1 && (a =2) == 1) putchar('T');...
9
by: find clausen | last post by:
How can I use this: if (!zxmes && self.name != "menu") and add if (zmes == 1) if (!zxmes && self.name != "menu" || zmes == 1) and make it work.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.