views:

638

answers:

3

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?

A: 

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++.

benefactual
A: 

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?

jrudolph
+4  A: 

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.

Matej