473,324 Members | 2,179 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,324 software developers and data experts.

AND and OR and parentheses

the following line appears in the K&R book in section 5.7:
leap = year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
my compiler suggests parentheses "around && within ||"

given that the AND operator has higher precedence than OR, would the
correct grouping be this:
leap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
?

thanks,
~rick

Nov 15 '05 #1
13 1888
rick wrote:
the following line appears in the K&R book in section 5.7:
leap = year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
my compiler suggests parentheses "around && within ||"
I hate that warning. Some compilers can be very fussy, even if the
precedence is obvious.
given that the AND operator has higher precedence than OR, would the
correct grouping be this:
leap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
?
Yes.
thanks,
~rick


Peter

Nov 15 '05 #2


rick wrote:
the following line appears in the K&R book in section 5.7:
leap = year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
my compiler suggests parentheses "around && within ||"

given that the AND operator has higher precedence than OR, would the
correct grouping be this:
leap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
?


Yes; your parentheses don't change the meaning of the
original. The compiler (presumably) issues the warning
because the people who wrote it felt that this is something
programmers often get wrong.

Oddly enough, in this particular case you *can't* get
it wrong! Consider the other possibility:

leap = year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);

This expression gives the same value to `leap' as the first
(equivalent) pair, even though it arrives at the value by a
different route.

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

Nov 15 '05 #3
Eric Sosman wrote:
rick wrote:
the following line appears in the K&R book in section 5.7:
leap = year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
my compiler suggests parentheses "around && within ||"

given that the AND operator has higher precedence than OR, would
the correct grouping be this:
leap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;


Yes; your parentheses don't change the meaning of the
original. The compiler (presumably) issues the warning
because the people who wrote it felt that this is something
programmers often get wrong.

Oddly enough, in this particular case you *can't* get
it wrong! Consider the other possibility:

leap = year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);

This expression gives the same value to `leap' as the first
(equivalent) pair, even though it arrives at the value by a
different route.


Actually that last is probably the most efficient, since 75% of the
time the value is resolved with one test, 24% with two tests, and
1% with three. I would use even more parens, as in:

leap = (year % 4 == 0) &&
((year % 100 != 0) || (year % 400 == 0));

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 15 '05 #4
CBFalconer wrote:
Eric Sosman wrote:
rick wrote:

the following line appears in the K&R book in section 5.7:
leap = year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
my compiler suggests parentheses "around && within ||"

given that the AND operator has higher precedence than OR, would
the correct grouping be this:
leap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;


Yes; your parentheses don't change the meaning of the
original. The compiler (presumably) issues the warning
because the people who wrote it felt that this is something
programmers often get wrong.

Oddly enough, in this particular case you *can't* get
it wrong! Consider the other possibility:

leap = year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);

This expression gives the same value to `leap' as the first
(equivalent) pair, even though it arrives at the value by a
different route.

Actually that last is probably the most efficient, since 75% of the
time the value is resolved with one test, 24% with two tests, and
1% with three. I would use even more parens, as in:

leap = (year % 4 == 0) &&
((year % 100 != 0) || (year % 400 == 0));


Yes, I think parentheses around the "boolean" (atomic) expressions
improve readability considerably, it's also customary in mathematics.
(However, I think relying on the precedence of `and' over `or' is
perfectly okay.)

August
Nov 15 '05 #5

"akarl" <fu********@comhem.se> wrote

Yes, I think parentheses around the "boolean" (atomic) expressions improve
readability considerably, it's also customary in mathematics. (However, I
think relying on the precedence of `and' over `or' is perfectly okay.)

The rule I use is that you can rely on your reader to know that * and / have
a higher precedence than + and -, and everything else needs parentheses.
Nov 15 '05 #6
"Malcolm" <re*******@btinternet.com> wrote:
"akarl" <fu********@comhem.se> wrote

Yes, I think parentheses around the "boolean" (atomic) expressions improve
readability considerably, it's also customary in mathematics. (However, I
think relying on the precedence of `and' over `or' is perfectly okay.)

The rule I use is that you can rely on your reader to know that * and / have
a higher precedence than + and -, and everything else needs parentheses.


Are you sure?

#include <stdio.h>

int main(void)
{
int i, j;

for ((i=1); (i<10); (i++)) {
(j=(i*10+3));
(printf(("Value nr. %d is %d\n"), (i), (j)));
}
}

Richard
Nov 15 '05 #7
On Wed, 17 Aug 2005 06:38:50 +0000, Richard Bos wrote:
"Malcolm" <re*******@btinternet.com> wrote:
"akarl" <fu********@comhem.se> wrote
>
> Yes, I think parentheses around the "boolean" (atomic) expressions improve
> readability considerably, it's also customary in mathematics. (However, I
> think relying on the precedence of `and' over `or' is perfectly okay.)
>

The rule I use is that you can rely on your reader to know that * and / have
a higher precedence than + and -, and everything else needs parentheses.


Are you sure?

#include <stdio.h>

int main(void)
{
int i, j;

for ((i=1); (i<10); (i++)) {
(j=(i*10+3));
(printf(("Value nr. %d is %d\n"), (i), (j)));
}
}


Well if we're going to take Malcolm's statement literally without applying
common sense, this would be even better (we could go indefinitely further):

#include <stdio.h>

int main(void)
{
int i, j;

for ((((i=1))); (((i<10))); (((i++)))) {
(((j=(((i*10+3))))));
(((printf(((("Value nr. %d is %d\n"))), (((i))), (((j)))))));
}
}

I think that both of these fail to capture his intent.

--
http://members.dodo.com.au/~netocrat

Nov 15 '05 #8

Netocrat wrote:
On Wed, 17 Aug 2005 06:38:50 +0000, Richard Bos wrote:
"Malcolm" <re*******@btinternet.com> wrote:
"akarl" <fu********@comhem.se> wrote
>
> Yes, I think parentheses around the "boolean" (atomic) expressions improve
> readability considerably, it's also customary in mathematics. (However, I
> think relying on the precedence of `and' over `or' is perfectly okay.)
>
The rule I use is that you can rely on your reader to know that * and / have
a higher precedence than + and -, and everything else needs parentheses.


He m[i/e]ssed up with the last part of the sentence. Here it is for
your
viewing pleasure:

With the usual exceptions, of course.

[snipped a lot of code from fertile minds]

C'mon guys -- be a sport.

Nov 15 '05 #9
On Wed, 17 Aug 2005 02:44:53 -0700, Suman wrote:
Netocrat wrote:
On Wed, 17 Aug 2005 06:38:50 +0000, Richard Bos wrote:
> "Malcolm" <re*******@btinternet.com> wrote:
>> "akarl" <fu********@comhem.se> wrote
>> >
>> > Yes, I think parentheses around the "boolean" (atomic) expressions
>> > improve readability considerably, it's also customary in
>> > mathematics. (However, I think relying on the precedence of `and'
>> > over `or' is perfectly okay.)
>> >
>> The rule I use is that you can rely on your reader to know that *
>> and / have a higher precedence than + and -, and everything else
>> needs parentheses.

He m[i/e]ssed up with the last part of the sentence. Here it is for your
viewing pleasure:

With the usual exceptions, of course.


Yes, obviously that's what Malcolm meant and he shouldn't be required to
write it (which he didn't according to my newsfeed).
[snipped a lot of code from fertile minds]

C'mon guys -- be a sport.
I thought I was being - the point of my post was that the literal and
strict interpretation of what was clearly a helpful guideline rather than
a formal rule was lacking in common sense and obviously wasn't what was
intended. Hence the first line of my post: "if we're going to [not apply]
common sense" and the final line:
I think that both of these fail to capture his intent.


--
http://members.dodo.com.au/~netocrat

Nov 15 '05 #10
Netocrat <ne******@dodo.com.au> wrote:
On Wed, 17 Aug 2005 06:38:50 +0000, Richard Bos wrote:
"Malcolm" <re*******@btinternet.com> wrote:
The rule I use is that you can rely on your reader to know that * and / have
a higher precedence than + and -, and everything else needs parentheses.


Are you sure?

#include <stdio.h>

int main(void)
{
int i, j;

for ((i=1); (i<10); (i++)) {
(j=(i*10+3));
(printf(("Value nr. %d is %d\n"), (i), (j)));
}
}


Well if we're going to take Malcolm's statement literally without applying
common sense,


My point was that, as he phrased it, Malcolm's statement doesn't really
allow common sense. Yes, my example of putting parens around a whole
assignment statement is a bit over the top; but I doubt that, in writing
"everything else", Malcolm had thought about = , ?: sizeof and so forth.
In fact, I'd say that he thought about | & ^ || && == != < > and that's
probably about it.

Richard
Nov 15 '05 #11

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote
>> The rule I use is that you can rely on your reader to know that * and
>> / have
>> a higher precedence than + and -, and everything else needs
>> parentheses.
>

My point was that, as he phrased it, Malcolm's statement doesn't really
allow common sense. Yes, my example of putting parens around a whole
assignment statement is a bit over the top; but I doubt that, in writing
"everything else", Malcolm had thought about = , ?: sizeof and so forth.
In fact, I'd say that he thought about | & ^ || && == != < > and that's
probably about it.

The other rule, which is just as important, is that the reader can't be
expected to cope with more than three layers of nesting. Unfortunately this
makes the "parenthese round everything ambiguous" rule hard to follow.

Personally I always give parenthese to sizeof() to make it look like a
function call, but I don't obvious write (sizeof(x)) which a literal
interpretation would demand.

The ++ operator is quirky because precedence and order of evaluation is the
same. It is so idiomatic that you have to use it in compound expressions,
though I prefer to put it on a line of its own where possible.
Nov 15 '05 #12
Malcolm wrote:
Personally I always give parenthese to sizeof()
to make it look like a function call,


I avoid giving parentheses to sizeof,
because I don't want a constant expression
to look like a function call.

--
pete
Nov 15 '05 #13
Malcolm wrote:
"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote
>The rule I use is that you can rely on your reader to know that * and
>/ have
>a higher precedence than + and -, and everything else needs
>parentheses.
My point was that, as he phrased it, Malcolm's statement doesn't really
allow common sense. Yes, my example of putting parens around a whole
assignment statement is a bit over the top; but I doubt that, in writing
"everything else", Malcolm had thought about = , ?: sizeof and so forth.
In fact, I'd say that he thought about | & ^ || && == != < > and that's
probably about it.


The other rule, which is just as important, is that the reader can't be
expected to cope with more than three layers of nesting. Unfortunately this
makes the "parenthese round everything ambiguous" rule hard to follow.

Who made this rule? There is nothing magic about three nesting levels.
Personally I always give parenthese to sizeof() to make it look like a
function call, but I don't obvious write (sizeof(x)) which a literal
interpretation would demand.
Why? It is not a function. If the argument needs parentheses I insert a
space 'sizeof (int)' so that it doesn't look like a function.
The ++ operator is quirky because precedence and order of evaluation is the
same. It is so idiomatic that you have to use it in compound expressions,
though I prefer to put it on a line of its own where possible.

What is 'quirky' about it? Sharing precedence level with sizeof is a
problem how?

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 15 '05 #14

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

Similar topics

44
by: seberino | last post by:
Tuples are defined with regards to parentheses ()'s as everyone knows. This causes confusion for 1 item tuples since (5) can be interpreted as a tuple OR as the number 5 in a mathematical...
3
by: Jane Doe | last post by:
Hello, I need to browse a list of hyperlinks, each followed by an author, and remove the links only for certain authors. 1. I searched the archives on Google, but didn't find how to tell the...
6
by: KathyB | last post by:
Hi, I'm passing a parameter to a script, which sometimes fails. However, I discovered that the ones failing had parentheses within the text string, e.g., "Step: Press the (0) on the console." ...
6
by: Michael Winter | last post by:
I'll be frank: what is the point? The use, and usefulness, of a set of capturing parentheses in a regular expression is clear. However, isn't an expression such as "/(?:x)/", exactly the same as...
1
by: Thomas F.O'Connell | last post by:
From 11.5 in the docs: "The syntax of the CREATE INDEX command normally requires writing parentheses around index expressions, as shown in the second example. The parentheses may be omitted when...
4
by: Thelma Lubkin | last post by:
I've inherited code with SELECT statements that look like this one: SELECT tblSales.YEAR As , Count(tblSales.UNITS) As FROM INNER JOIN tblSales ON .STFID = tblSales.STFID WHERE...
3
by: Lyn | last post by:
Can anyone explain this for me? A sub procedure can be called with or without parentheses around the arguments. By personal convention, I normally use parentheses. I am in the middle of a...
3
by: Helen Wheels | last post by:
Can we use parentheses in a check constraint in MS-SQL-server DDL? e.g. I'm having a problem with the following statement: ALTER TABLE . ADD CONSTRAINT CHECK (( IS NULL AND IS NULL) OR (...
7
by: vlsidesign | last post by:
I am not sure how to think about parentheses. It seems when they are used alongside characters it indicates a function, like addTwoNums(). But is also is used to enforce which operators and...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.