What is the difference between priority, associativity and order?

This confusion arises when most people learn to evaluate arithmetic expressions using the PEDMAS or BODMAS rules , while arithmetic expressions in programming languages ​​such as C # do not work the same way.

What do you do?

+4
source share
3 answers

Priority rules determine the priority of operators (which operators will be ranked first, for example, multiplication has a higher priority than adding, PEMDAS).

Associativity rules show how operators with the same priority are grouped. Arithmetic operators are left-associative, but the assignment is correct associative (for example, a = b = c will be evaluated as b = c, a = b).

The order is the result of applying the rules of priority and associativity and tells how the expression will be evaluated - which operators will be evaluated, and then what at the end. The actual order can be changed using curly braces (curly braces are also the operator with the highest priority).

The priority and associativity of operators in a programming language can be found in its manual on the language or specification.

+8
source

I'm not sure there really is a difference. Traditional BODMAS (brackets, orders, division, multiplication, addition, subtraction) or PEDMAS (parentheses, indicators, division, multiplication, addition, subtraction) are simply subsets of all possible operations and indicate the order in which such operations should be applied in I do not I know not a single language in which the BODMAS / PEDMAS rules are violated, but in each language various other operators are usually added, such as ++, -, =, etc.

I always keep the operator's priority list close to the point in case of confusion. However, when in doubt, it is usually worthwhile to use some parentheses to make the meaning clear. Just keep in mind that parentheses do not have the highest priority - see http://msdn.microsoft.com/en-us/library/126fe14k.aspx for an example in C ++.

+2
source

Priority and associativity determine how and in what order the term should be divided into sub-terms. In other words, it defines rules in which brackets must be set implicitly unless explicitly specified.

If you have a term without parentheses, you start with the operators with the lowest precedence and enclose them in parentheses.

For instance:

Precendences:

  • .
  • * /
  • +, -
  • ==
  • &&

Term:

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

will be grouped as follows:

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

One very common rule is the priority * and / before + and -.

Associativity determines in which direction operators with the same priority are grouped. Most operators are from left to right. Unary prefix operators from right to left.

Example:

 1 + 2 + 3 + 4 

grouped as follows:

  • (1 + 2) + 3 + 4
  • ((1 + 2) + 3) + 4
  • (((1 + 2) + 3) + 4)

a

!! + 1

grouped as

  • !! (+ 1)
  • ! (! (+ 1))
  • (! (! (+ 1)))

Does everything still comply with the BODMAS / PEDMAS rules you encountered?

+2
source

Source: https://habr.com/ru/post/1276740/


All Articles