优先级、结合性和顺序之间有什么区别?

发布于 2024-07-05 10:11:58 字数 188 浏览 7 评论 0原文

这种混乱的出现是因为大多数人都接受过根据 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不羁少年 2024-07-12 10:11:58

优先规则指定运算符的优先级(首先评估哪些运算符,例如乘法的优先级高于加法,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.

寒江雪… 2024-07-12 10:11:58

我不确定是否真的有区别。 传统的 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++.

清醇 2024-07-12 10:11:58

优先级和关联性都指定术语应如何以及以何种顺序拆分为子术语。 换句话说,如果没有明确指定,它是否指定隐式设置括号的规则。

如果您有一个不带括号的术语,则从优先级最低的运算符开始并将其括在括号中。

例如:

优先级:

  1. *,/
  2. +,-
  3. ==
  4. &&

术语:

!person.isMarried && person.age == 25 + 2 * 5

将这样分组:

  1. !(person.isMarried) && (person.age) == 25 + 2 * 5
  2. (!(person.isMarried)) && (person.age) == 25 + 2 * 5
  3. (!(person.isMarried)) && (person.age) == 25 + (2 * 5)
  4. (!(person.isMarried)) && (person.age) == (25 + (2 * 5))
  5. (!(person.isMarried)) && ((person.age) == (25 + (2 * 5)))
  6. ((!(person.isMarried)) && ((person.age) == (25 + (2 * 5))))

一个非常常见的规则是 * 和 / 优先于 + 和 - 。

关联性指定相同优先级的运算符按哪个方向分组。 大多数运算符都是从左到右的。 一元前缀运算符是从右到左的。

示例:

1 + 2 + 3 + 4

分组如下:

  1. (1 + 2) + 3 + 4
  2. ((1 + 2) + 3) + 4
  3. (((1 + 2) + 3) + 4)

!!+1

分组为

  1. !! (+1)
  2. !(!(+1))
  3. (!(!(+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:

  1. .
  2. !
  3. *,/
  4. +,-
  5. ==
  6. &&

The term:

!person.isMarried && person.age == 25 + 2 * 5

would be grouped like that:

  1. !(person.isMarried) && (person.age) == 25 + 2 * 5
  2. (!(person.isMarried)) && (person.age) == 25 + 2 * 5
  3. (!(person.isMarried)) && (person.age) == 25 + (2 * 5)
  4. (!(person.isMarried)) && (person.age) == (25 + (2 * 5))
  5. (!(person.isMarried)) && ((person.age) == (25 + (2 * 5)))
  6. ((!(person.isMarried)) && ((person.age) == (25 + (2 * 5))))

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:

1 + 2 + 3 + 4

is grouped like that:

  1. (1 + 2) + 3 + 4
  2. ((1 + 2) + 3) + 4
  3. (((1 + 2) + 3) + 4)

while

!!+1

is grouped as

  1. !!(+1)
  2. !(!(+1))
  3. (!(!(+1)))

So far everything complies to the BODMAS/PEDMAS rules which differences have you experienced?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文