运算符优先级与&&和逗号 [英] operator precedence with && and comma

查看:83
本文介绍了运算符优先级与&&和逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如果有下列表达式:

c = a&& --b;

如果a为0,b未评估且c直接变为0.

这是否意味着&&运算符优先于

'' - ''运算符?

与优先表中提到的相反?

另外,

用逗号操作符。

考虑,

a = b,a = a * 4,a = a / 5;

这里,

根据优先级表的优先顺序是/ then * then =

和最后一个逗号。但是在实践中将从左到右。这个就像

从左到右编写单独的语句。

这是否意味着逗号优先于其他

运算符。如此它应该位于优先位置的顶部。

关于这个主题的任何亮点都将有助于清除这个问题。

-siliconwafer

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 opposed to what is mentioned in precedence table?
Also,
with comma operator.
consider,
a= b,a = a*4,a = a/5;
Here,
order of precedence according to precedence table is / then * then =
and last comma.but will be from left to right in practice.This is like
writing seperate statements from left to right.
Does this mean that comma is given the hiegest precedence over other
operators.if so it should sit at the top of precedence.
any light on the subject will be highly helpful to clear the anamoly.
-siliconwafer

推荐答案



siliconwafer写道:


siliconwafer wrote:

以下表达式:
c = a&& --b;
如果a为0,则b未被评估,c直接变为0.
这是否意味着&&运算符优先于
'' - ''运算符?


No.

与优先表中提到的相反?


相信桌子。

另外,用逗号操作员
考虑,
a = b,a = a * 4,a = a / 5;
这里,根据优先级表的优先顺序是/ then * then =
和最后一个逗号。但是在实践中将从左到右。这就像从左到右编写单独的语句。
这是否意味着逗号被赋予了优于其他
运算符的优先级。如果它应该位于优先级的顶部。<关于这个问题的任何亮点对于清除这些问题非常有帮助。
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?
No.
as opposed to what is mentioned in precedence table?
Believe the table.
Also,
with comma operator.
consider,
a= b,a = a*4,a = a/5;
Here,
order of precedence according to precedence table is / then * then =
and last comma.but will be from left to right in practice.This is like
writing seperate statements from left to right.
Does this mean that comma is given the hiegest precedence over other
operators.if so it should sit at the top of precedence.
any light on the subject will be highly helpful to clear the anamoly.




你误解了优先权一词。要获得熟悉的理由,请考虑a + b * c。哪个

+和*具有更高的优先级:''s'执行的那个

first"或最后执行的那个?


然而,整个业务都是红鲱鱼。优先级

和关联性(这对于语言标准中的语法来说实际上只是一个方便的短 -

)确定了表达式的含义

,但不确定

评估的顺序。事实上&& "短路"评价

是&&&与其他运营商相比,运营商本身,而不是其优先级

。考虑一下:'0 * --x''没有

短路,即使优先关系只是

相同。


运营商&&和||并且,......:提供一些特殊的

关于排序和短路的保证;这些都与他们的优先级或关联性无关。


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



You''re misunderstanding the term "precedence." To
get back on familiar ground, consider `a+b*c''. Which of
+ and * has higher precedence: the one that''s "executed
first" or the one that''s "executed last?"

However, the whole business is a red herring. Precedence
and associativity (which are really just a convenient short-
hand for the grammar in the language Standard) determine the
meaning of expressions, but do not determine the order of
evaluation. The fact that && "short-circuits" the evaluation
is a property of the && operator itself, not of its precedence
as compared to other operators. Consider: `0 * --x'' does not
short-circuit, even though the precedence relations are just
the same.

The operators && and || and , and ?...: provide some special
guarantees about sequencing and short-circuiting; these have
nothing to do with their precedence or associativity.

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


siliconwafer写道:
siliconwafer wrote:

如果有下列表达式:
c = a&& --b;
如果a为0,则b未被评估,c直接变为0.
这是否意味着&&运算符优先于
'' - ''运算符?


编号优先顺序是一个与评估顺序不同的概念。

优先权只是一种描述表达式的方式
$ b其他表达式的$ b操作数。

用逗号运算符。
考虑,
a = b,a = a * 4,a = a / 5;
这里,根据优先级表的优先顺序是/ then * then =
和最后一个逗号。


所以逗号是最弱的约束力;其他每一个都是操作数

逗号。

但实际上是从左到右。
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?
No. Precedence is a separate concept from order-of-evaluation.
Precedence is just a way of dsescribing which expressions are
operands to which other expressions.
with comma operator.
consider,
a= b,a = a*4,a = a/5;
Here,
order of precedence according to precedence table is / then * then =
and last comma.
So comma is the most weakly binding; everying else are operands
to comma.
but will be from left to right in practice.




*评估顺序一个逗号表达式A,B是

来评估A,然后评估B.优先级无关

with it。


-

Chris" electric hedgehog" Dollin

预测自我预测者的预测是可预测的不可预测的。



The *order of evaluation" of a comma-expression `A, B` is
to evaluate A first, then evaluate B. Precedence has nothing to do
with it.

--
Chris "electric hedgehog" Dollin
predicting self-predictors'' predictions is predictably unpredictable.




Chris Dollin写道:

Chris Dollin wrote:
siliconwafer写道:
siliconwafer wrote:

如果有以下表达式:
c = a&& --b;
如果a为0,则b未被评估,c直接变为0.
这是否意味着&&运算符优先于
'' - ''运算符?
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?



No.优先级是一个与评估顺序不同的概念。
优先级只是一种方法来描述哪些表达式是其他表达式的操作数。



No. Precedence is a separate concept from order-of-evaluation.
Precedence is just a way of dsescribing which expressions are
operands to which other expressions.

用逗号运算符。
考虑,
a = b,a = a * 4,a = a / 5;
这里,根据优先级表的优先顺序是/ then * then =
和最后一个逗号。
with comma operator.
consider,
a= b,a = a*4,a = a/5;
Here,
order of precedence according to precedence table is / then * then =
and last comma.



所以逗号是最弱的约束力;其他的都是操作数
逗号。



So comma is the most weakly binding; everying else are operands
to comma.

但在实践中将从左到右。
but will be from left to right in practice.



评估的顺序和;一个逗号表达式A,B用于评估A,然后评估B.优先级与它无关。

-
克里斯电刺猬 Dollin
预测自我预测者的预测可以预测是不可预测的。



The *order of evaluation" of a comma-expression `A, B` is
to evaluate A first, then evaluate B. Precedence has nothing to do
with it.

--
Chris "electric hedgehog" Dollin
predicting self-predictors'' predictions is predictably unpredictable.




你好克里斯,

所以如果''优先'''不同的是评价顺序,是否意味着

评价顺序总是从左到右,无论什么

关联/优先表明?

如果是这样,&&&&或者,重要的是什么?

''优先''是什么?



Hi chris,
So if ''precedence'' is different that ''order of evaluation'',does it mean
that order of evaluation is always from left to right,no matter what
the associativiy/precedence indicate?
If so,when does the ''order'' of && or , matter and when does their
''precedence''matter?


这篇关于运算符优先级与&amp;&amp;和逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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