基本问题 [英] Fundamental question

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

问题描述

a&& b || c


被评估为


(a&& b)|| c


或评估为


a&& (b || c)


理论和逻辑说它是(a&& b)|| c

这还取决于compilar吗?

我不这么认为。

在我看来,第一个是最终答案。

你的人说什么?


Ajay

解决方案

周五, 2004年6月4日15:48:30 +0530,myName写道:

a&& b || c

评估为

(a&& b)|| c

或评估为&&& (b || c)


&&运算符的优先级高于||。因此,&& b首先评估
。这使得

a&& b || c

被评估为

(a&& b)|| c

这还取决于compilar吗?


否。这取决于标准。标准说它是(a&& b)|| c。

我不这么认为。


你认为是对的。

在我看来,第一个是最终的答案。
你的人说什么?



我说第一个是唯一的答案。


-

int main(void){int putchar(int ),i = 0;无符号长t = 500555079,n [] = {t

,159418370,88921539,286883974,80500161,0};而(n [i])putchar(*(! (t& 1

)+!(t ||!++ i)+"#\ n")),(t&& 1+(t>> = i-) 〜-i))||(t = n [i] ^ n [i-1]);返回0;}


星期五,4 2004年6月,myName写道:

a&& b || c
评估为
(a&& b)|| c
或评估为&& amp; (b || c)

理论和逻辑说它是(a&& b)|| c




这在每个符合标准的编译器上都是正确的。


我猜逻辑是0和1的值,& ;&安培;类似于

乘法和||类似于(饱和)加法,这是

反映在它们的相对优先级中。


(可能另一个有用的类比是min / max但是C没有'' t $ / $
有那些运营商..)


Pieter Droogendijk< gi *** @ binSPAMky.homFOReunix.orMEg>写道:

2004年6月4日星期五15:48:30 +0530,myName写道:

a&& b || c

评估为

(a&& b)|| c

或评估为&&& (b || c)



&&运算符的优先级高于||。因此,&&首先评估b。这使得
&& b || c
评估为
(a&& b)|| c




请注意:与大多数其他运营商的情况不同,这些操作数

按顺序,从左到右,以及何时进行评估结果可以是

从已经评估的操作数确定,

其他人不会。例如,


a * b - c


评估的顺序可以是:首先是c,然后是b,然后是a,然后是

乘法,然后减法。另一个可能是:第一个b,然后是a,

然后是乘法,然后是c,然后是减法。您知道订单的唯一事情是,显然,操作数必须在操作执行之前进行评估。乘法

将在减法之前发生。然而,在


a&& b || c / /

订单_must_是:


评估一个

如果a,那么

评价b

合乎逻辑且

其他

合理的结果,保证为0

b是未评估

如果结果为0

评估c

合乎逻辑或

其他

逻辑或保证导致1

c未评估


情况也是如此,而a || b&& c具有||的_value_ (b

&& c),操作数仍按从左到右的顺序进行评估;也就是说,如果a非零,那么
,整个表达式都是真的,并且b和

c都不需要或将被评估。


这是一个很有用的功能,因为你可以做一些事情,比如


if(ptr!= NULL&& * ptr< MAX_VALUE)


而不用担心* ptr可能会在您检查之前进行评估

ptr不为空。


Richard


a && b || c

is evaluated as

(a && b) || c

or it is evaluated as

a && (b || c)

The theory and logic says it''s (a && b) || c
Does it depends on compilar also ?
I don''t think so.
In my view the 1st is the ultimate answer.
What you people say ?

Ajay

解决方案

On Fri, 04 Jun 2004 15:48:30 +0530, myName wrote:

a && b || c

is evaluated as

(a && b) || c

or it is evaluated as

a && (b || c)
The && operator has a higher precedence than ||. Therefore, a && b is
evaluated first. That makes that
a && b || c
is evaluated as
(a && b) || c
Does it depends on compilar also ?
No. It depends on the standard. And the standard says it''s (a && b) || c.
I don''t think so.
You thought right.
In my view the 1st is the ultimate answer.
What you people say ?



I say the first is the only answer.

--
int main(void){int putchar(int),i=0;unsigned long t=500555079,n[]={t
,159418370,88921539,286883974,80500161,0};while(n[i])putchar(*(!(t&1
)+!(t||!++i)+"# \n")),(t&&1+(t>>=i-~-i))||(t=n[i]^n[i-1]);return 0;}


On Fri, 4 Jun 2004, myName wrote:

a && b || c
is evaluated as
(a && b) || c
or it is evaluated as
a && (b || c)

The theory and logic says it''s (a && b) || c



This is correct on every conforming compiler.

I guess the logic is that with values 0 and 1, && is similar to
multiplication and || similar to (saturated) addition and this is
reflected in their relative precedence.

(possibly another useful analogy would be min/max but C doesn''t
have operators for those..)


Pieter Droogendijk <gi***@binSPAMky.homFOReunix.orMEg> wrote:

On Fri, 04 Jun 2004 15:48:30 +0530, myName wrote:

a && b || c

is evaluated as

(a && b) || c

or it is evaluated as

a && (b || c)



The && operator has a higher precedence than ||. Therefore, a && b is
evaluated first. That makes that
a && b || c
is evaluated as
(a && b) || c



And note: unlike what is true for most other operators, these operands
are evaluated in order, left to right, and when the outcome can be
determined from the operands that have already been evaluated, the
others won''t be. For example, in

a*b - c

the order of evaluation could be: first c, then b, then a, then the
multiplication, then the subtraction. Another could be: first b, then a,
then the multiplication, then c, then the subtraction. The only things
you know about the order is that, obviously, the operands must be
evaluated before their operations are performed; and the multiplication
will happen before the subtraction. However, in

a && b || c

the order _must_ be:

evaluate a
if a, then
evaluate b
do logical and
else
result of logical and is guaranteed to be 0
b is not evaluated
if result of and is 0
evaluate c
do logical or
else
logical or is guaranteed to result in 1
c is not evaluated

It is also the case that, while a || b && c has the _value_ of a || (b
&& c), the operands are still evaluated in left-to-right order; that is,
if a is non-zero, the whole of the expression is true, and neither b nor
c need or will be evaluated.

This is a useful feature, because you can do things like

if (ptr!=NULL && *ptr<MAX_VALUE)

without worrying that *ptr might be evaluated before you''ve checked that
ptr is not null.

Richard


这篇关于基本问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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