优先级、结合性和顺序之间有什么区别?
这种混乱的出现是因为大多数人都接受过根据 PEDMAS 或 BODMAS 规则来评估算术表达式的培训而 C# 等编程语言中的算术表达式的工作方式则不同。
您对此有何看法?
This confusion arises as most people are trained to evaluate arithmetic expressions as per PEDMAS or BODMAS rule whereas arithmetic expressions in programming languages like C# do not work in the same way.
What are your takes on it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
优先规则指定运算符的优先级(首先评估哪些运算符,例如乘法的优先级高于加法,PEMDAS)。
结合性规则告诉我们如何对相同优先级的运算符进行分组。 算术运算符是左关联的,但赋值是右关联的(例如,a = b = c 将被计算为 b = c、a = b)。
顺序是应用优先级和关联性规则的结果,并告诉表达式将如何计算 - 哪些运算符将首先被计算,哪些运算符将被计算后来,最后。 实际的顺序可以通过使用大括号来改变(大括号也是优先级最高的运算符)。
编程语言中运算符的优先级和结合性可以在其语言手册或规范中找到。
Precedence rules specify priority of operators (which operators will be evaluated first, e.g. multiplication has higher precedence than addition, PEMDAS).
The associativity rules tell how the operators of same precedence are grouped. Arithmetic operators are left-associative, but the assignment is right associative (e.g. a = b = c will be evaluated as b = c, a = b).
The order is a result of applying the precedence and associativity rules and tells how the expression will be evaluated - which operators will be evaluated firs, which later, which at the end. The actual order can be changed by using braces (braces are also operator with the highest precedence).
The precedence and associativity of operators in a programming language can be found in its language manual or specification.
我不确定是否真的有区别。 传统的 BODMAS(括号、阶数、除法、乘法、加法、减法)或 PEDMAS(括号、指数、除法、乘法、加法、减法)只是所有可能运算的子集,并表示这些运算应应用的顺序我不知道有哪种语言违反了 BODMAS/PEDMAS 规则,但每种语言通常都会添加各种其他运算符 - 例如 ++、--、= 等。
我总是随身携带一个运算符优先级列表。以防混乱。 然而,当有疑问时,通常值得使用一些括号来使含义清晰。 请注意,括号没有最高优先级 - 请参阅 http://msdn .microsoft.com/en-us/library/126fe14k.aspx 有关 C++ 示例。
I am not sure there really is a difference. The traditional BODMAS (brackets, orders, division, multiplication, addition, subtraction) or PEDMAS (parentheses, exponents, division, multiplication, addition, subtraction) are just subsets of all the possible operations and denote the order that such operations should be applied in. I don't know of any language in which the BODMAS/PEDMAS rules are violated, but each language typically adds various other operators - such as ++, --, = etc.
I always keep a list of operator precedence close to hand in case of confusion. However when in doubt it is usually worth using some parentheses to make the meaning clear. Just be aware that parentheses do not have the highest precedence - see http://msdn.microsoft.com/en-us/library/126fe14k.aspx for an example in C++.
优先级和关联性都指定术语应如何以及以何种顺序拆分为子术语。 换句话说,如果没有明确指定,它是否指定隐式设置括号的规则。
如果您有一个不带括号的术语,则从优先级最低的运算符开始并将其括在括号中。
例如:
优先级:
术语:
将这样分组:
一个非常常见的规则是 * 和 / 优先于 + 和 - 。
关联性指定相同优先级的运算符按哪个方向分组。 大多数运算符都是从左到右的。 一元前缀运算符是从右到左的。
示例:
分组如下:
而
!!+1
分组为
到目前为止,一切都符合 BODMAS/PEDMAS 规则,您遇到了哪些差异?
Precedence and associativity both specify how and in which order a term should be split into subterms. In other words does it specifies the rules where brackets are to be set implicitly if not specified explicitly.
If you've got a term without brackets, you start with operators with lowest precedence and enclose it in brackets.
For example:
Precendences:
The term:
would be grouped like that:
One very common rule is the precedence of * and / before + and - .
Associativity specifies in which direction operators of the same precedence are grouped. Most operators are left-to-right. Unary prefix operators are right-to-left.
Example:
is grouped like that:
while
!!+1
is grouped as
So far everything complies to the BODMAS/PEDMAS rules which differences have you experienced?