Common operator notation: Difference between revisions

Content deleted Content added
Fixed a typo: "operator" not "operatior."
Tehjar (talk | contribs)
No edit summary
Line 8:
Each operator is given a position, precedence, and an associativity. The '''operator precedence''' is a number (from high to low or vice versa) that defines which operator that takes an operand surrounded by two operators of different precedence (or priority). Multiplication normally has higher precedence than addition, for example, so 3+4×5 = 3+(4×5) ≠ (3+4)×5.
 
In terms of operator position, an operator may be prefix, postfix, or infix. A prefix operator immediately precedes its operand, as in −x. A postfix operator immediately succeeds its operand, as in x! for instance. An infix operator is positioned in between a left and a right operand, as in x+y. Some languages, most notably the C-syntax family, stretches this conventional terminology and speaks also of ''ternary'' infix operators (a?b:c). Theoretically it would even be possible (but not necessarily practical) to define parenthesization as ana n-aryunary bifix operation.
 
'''[[Operator associativity]]''', determines what happens when an operand is surrounded by operators of the same precedence, as in 1-2-3: An operator can be left-associative, right-associative, or non-associative. Left-associative operators are applied to operands in left-to-right order while right-associative operators are the other way round. The basic arithmetic operators are normally all left-associative, which means that 1-2-3 = (1-2)-3 ≠ 1-(2-3), for instance. In programming languages where assignment is implemented as an operator, that operator is often right-associative. If so, a statement like a := b := c would be equivalent to a := (b := c), which means that the value of c is copied to b which is then copied to a. An operator which is non-associative cannot compete for operands with operators of equal precedence. In [[Prolog]] for example, the infix operator :- is non-associative, so constructs such as a :- b :- c are syntax errors.