Order of Operations - Programming Languages

Programming Languages

Many programming languages use precedence levels that conform to the order commonly used in mathematics, though some, such as APL and Smalltalk, have no operator precedence rules (in APL evaluation is strictly right to left, in Smalltalk it's strictly left to right).

The logical bitwise operators in C (and all programming languages that borrowed precedence rules from C, for example, C++, Perl and PHP) have a precedence level that the creator of the C language considers to be unsatisfactory. However, many programmers have become accustomed to this order. The relative precedence levels of operators found in many C-style languages are as follows:

1 -> . :: Grouping, scope, array/member access
2 ! ~ - + * & sizeof type cast ++x --x (most) unary operations, sizeof and type casts
3 * / % Multiplication, division, modulo
4 + - Addition and subtraction
5 << >> Bitwise shift left and right
6 < <= > >= Comparisons: less-than, ...
7 == != Comparisons: equal and not equal
8 & Bitwise AND
9 ^ Bitwise exclusive OR
10 | Bitwise inclusive (normal) OR
11 && Logical AND
12 || Logical OR
13 ?: = += -= *= /= %= &= |= ^= <<= >>= Conditional expression (ternary) and assignment operators
14 , Comma operator

Examples:

  • !A + !B(!A) + (!B)
  • ++A + !B(++A) + (!B)
  • A + B * CA + (B * C)
  • A || B && CA || (B && C)
  • (A && B == C)(A && (B == C) )

The accuracy of software developer knowledge about binary operator precedence has been found to closely follow their frequency of occurrence in source code.

Read more about this topic:  Order Of Operations

Famous quotes containing the words programming and/or languages:

    If there is a price to pay for the privilege of spending the early years of child rearing in the driver’s seat, it is our reluctance, our inability, to tolerate being demoted to the backseat. Spurred by our success in programming our children during the preschool years, we may find it difficult to forgo in later states the level of control that once afforded us so much satisfaction.
    Melinda M. Marshall (20th century)

    I am always sorry when any language is lost, because languages are the pedigree of nations.
    Samuel Johnson (1709–1784)