AND和OR和括号 [英] AND and OR and parentheses

查看:154
本文介绍了AND和OR和括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下一行出现在第5.7节的K& R书中:

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

我的编译器建议使用括号around&&在||"


内,鉴于AND运算符的优先级高于OR,

正确的分组是否为:

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




谢谢,

~里克

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

推荐答案

rick写道:
rick wrote:
以下一行出现在5.7节的K& R书中:
leap =年%4 == 0&&年%100!= 0 ||年%400 == 0;
我的编译器建议使用括号around&& amp;在||中


我讨厌这个警告。有些编译器非常挑剔,即使

优先级是显而易见的。

假设AND运算符的优先级高于OR,
是否正确分组这个:
leap =(年%4 == 0&&年%100!= 0)||年%400 == 0;



是的。

谢谢,
〜里克
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




彼得






rick wrote:
以下一行出现在K& R书中第5.7节:
leap = year%4 == 0&&年%100!= 0 ||年%400 == 0;
我的编译器建议使用括号around&& amp;在||"

内,如果AND运算符的优先级高于OR,则
正确的分组是否为:
leap =(year%4 == 0& & year%100!= 0)||年%400 == 0;
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;
?




是;你的括号不会改变

原文的含义。编译器(大概)会发出警告

,因为编写它的人认为这是某些事情

程序员经常出错。


奇怪的是,在这种特殊情况下你*不能*得到

错了!考虑另一种可能性:


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


这个表达式给第一个'leap''提供相同的值

(等效)对,即使它通过

不同路线到达该值。


-
呃********* @ sun.com



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

Eric Sosman写道:
Eric Sosman wrote:
rick写道:
rick wrote:
以下一行出现在第5.7节的K& R书中:
飞跃=年%4 == 0&&年%100!= 0 ||年%400 == 0;
我的编译器建议使用括号around&& amp;在||"

内,如果AND运算符的优先级高于OR,那么正确的分组是这样的:
leap =(year%4 == 0& & year%100!= 0)||年%400 == 0;
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;



是的;你的括号不会改变
原文的含义。编译器(大概)会发出警告
因为编写它的人认为这是程序员经常出错的事情。

奇怪的是,在这种特殊情况下你*可以'*得到它错了!考虑另一种可能性:

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

这个表达式给第一个
(等效)对的'leap''提供相同的值,即使它通过不同的路线到达价值。



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.




实际上,最后可能效率最高,因为75%的

时间值通过一次测试得到解决,24%用两次测试解决,

1%用3次。我会使用更多的parens,例如:


leap =(年%4 == 0)&&

((年%100! = 0)||(年%400 == 0));


-

"如果您想通过groups.google发布后续内容.com,不要使用

破碎的回复链接在文章的底部。点击

" show options"在文章的顶部,然后点击

回复在文章标题的底部。 - Keith Thompson



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


这篇关于AND和OR和括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆