Operator precedence: Difference between revisions

Add Ecstasy table
(→‎{{header|Phix}}: added the new operators for 0.8.2)
(Add Ecstasy table)
 
(46 intermediate revisions by 21 users not shown)
Line 10:
State whether arguments are passed by value or by reference.
<br><br>
 
=={{header|11l}}==
See [http://11l-lang.org/doc/operators 11l documentation].
 
=={{header|8th}}==
In 8th it's very simple: the currently invoked word has precedence, and items are operated upon in the order they are popped off the stack.
 
=={{header|6502 Assembly}}==
There are two types of indirect addressing: Indirect X and Indirect Y. These differ in that the indirect X mode applies the offset before looking up the address.
 
<syntaxhighlight lang="6502asm">LDX #$02
LDY #$03
 
LDA ($20,x) ;uses the values stored at $20+x and $21+x as a memory address, and reads the byte at that address.
LDA ($20),y ;uses the values store at $20 and $21 as a memory address, and reads the byte at that address + Y.</syntaxhighlight>
 
The use of parentheses for these modes is required, and nicely illustrates the order of operations. To put it in terms of a hierarchy:
<pre>
Highest: Indirection: ()
Middle: Offsetting: ,x ,y
Lowest: Reading at the specified address.
</pre>
 
=={{header|Ada}}==
Line 205 ⟶ 224:
| 12 || or || Left to right || Logical or ||
|}
 
=={{header|Arturo}}==
 
There is no need for operator precedence in Arturo, since all expressions are parsed/evaluated in the exact same way.
 
The main expression evaluation order of Arturo is right-to-left. But with a tiny asterisk: Your code will be evaluated from left to right, it is the expressions passed to your function calls that will be evaluated from right-to-left.
 
This works for mathematical expressions too, since they are treated as normal function calls.
 
See [https://arturo-lang.io/documentation/language/#precedence-and-evaluation Arturo documentation].
 
<syntaxhighlight lang="rebol">print 2 + 3 * 5 ; same as 2 + (3 * 5)
print 3 * 5 + 2 ; same as 3 * (5 + 2)</syntaxhighlight>
 
{{out}}
 
<pre>17
21</pre>
 
=={{header|AWK}}==
See also: [https://www.gnu.org/software/gawk/manual/html_node/Precedence.html#Precedence gawk-Reference]
<syntaxhighlight lang="awk">
<lang AWK>
# Operators are shown in decreasing order of precedence.
# A blank line separates groups of operators with equal precedence.
Line 270 ⟶ 307:
# ^= exponentiation assignment
# **= exponentiation assignment (not all awk's)
</syntaxhighlight>
</lang>
 
=={{header|BASIC256}}==
Operators are evaluated according to a strict set of rules.
These rules are called the “Order of Operations”.
 
{| class="wikitable"
! Priority || Operator(s) || Category/Description
|-
| ALIGN=CENTER|highest || () ||Grouping
|-
| ALIGN=CENTER|10 || ^ ||Exponent
|-
| ALIGN=CENTER| 9 || - ~ ||Unary Minus and Bitwise Negation (NOT)
|-
| ALIGN=CENTER| 8 || * / \ ||Multiplication, Division, and Integer Division
|-
| ALIGN=CENTER| 7 || % ||Integer Remainder (Mod)
|-
| ALIGN=CENTER| 6 || + - ; ||Addition/Concatenation, and Subtraction
|-
| ALIGN=CENTER| 5 || & <nowiki>|</nowiki> ||Bitwise And and Bitwise Or
|-
| ALIGN=CENTER| 4 || < <= > >= = <> ||Comparison (Numeric and String)
|-
| ALIGN=CENTER| 3 || NOT ||Logical Not
|-
| ALIGN=CENTER| 2 || AND ||Logical And
|-
| ALIGN=CENTER| 1 || OR ||Logical Or
|-
| ALIGN=CENTER|lowest || XOR ||Logical Exclusive Or
|}
 
 
=={{header|bc}}==
Line 298 ⟶ 368:
|-
| Lowest || <nowiki>||</nowiki> || Logical Or || Left to right
|}
 
=={{header|BCPL}}==
In the table below, L indicates left associativity and R indicates right associativity.
{| class="wikitable"
! Priority !! Operator !! Notes
|-
|| 9 || Names, Literals, <code>?</code>
|-
|| || <code>TRUE</code>, <code>FALSE</code>, <code>BITSPERBCPLWORD</code>
|-
|| || (E)
|-
|| 9L || <code>SLCT</code>, <code>:</code> || Field selectors
|-
|| || Function and method calls
|-
|| || Subscripted expressions using <code>[</code> and <code>]</code>
|-
|| 8L || <code>!</code>, <code>%</code>, <code>OF</code> || Dyadic
|-
|| 7 || <code>!</code>, <code>@</code> || Prefixed
|-
|| 6L || <code>*</code>, <code>/</code>, <code>MOD</code>
|-
|| 5 || <code>+</code>, <code>-</code>, <code>ABS</code> || Dyadic and monadic
|-
|| 4 || <code>=</code>, <code>~=</code>, <code><=</code>, <code>>=</code>, <code><</code>, <code>></code> || Extended relations
|-
|| 4L || <code><<</code>, <code>>></code> || Bit shift operators
|-
|| 3 || <code>~</code>
|-
|| 3L || <code>&</code>
|-
|| 2L || <code><nowiki>|</nowiki></code>
|-
|| 1L || <code>EQV</code>, <code>XOR</code>
|-
|| 1R || <code>-> ,</code> || Conditional expression
|-
|| 0 || <code>VALOF</code>, <code>TABLE</code>
|}
=={{header|BQN}}==
 
The operators of most popular programming languages correspond to functions in BQN (all functions, builtin and defined, are infix).
 
However, modifiers (higher order functions) have higher precedence over functions.
 
Here, <code>2 + 1</code> is evaluated first:
<syntaxhighlight lang="bqn">3 * 2 + 1</syntaxhighlight>
 
Here, <code>-˜</code> is evaluated first. <code>˜</code> flips the arguments given to a function.
<syntaxhighlight lang="bqn">3 * 2 -˜ 1</syntaxhighlight>
 
A precedence table for all BQN syntax is shown below.
 
{| class="wikitable"
! Precedence !! Role !! Associativity !! Examples and comments
|-
| Highest || Brackets || || <code>()⟨⟩{}[]</code>
|-
| || <code>.</code> || Left || Namespace field access
|-
| || <code>‿</code> || || Stranding (forms lists); really an n-ary instead of binary operator
|-
| || Modifier || Left || e.g. <code>∘⎉¨´</code>, also modified assignment <code>↩</code> in <code>Fn↩</code>
|-
| || Function || Right || e.g. <code>+↕⊔⍉</code> (including in trains), also assignment <code>←↩⇐</code>
|-
| Lowest || Separator || || <code>⋄,</code> and newline, and block punctuation <code>;:?</code>
|}
 
Line 591 ⟶ 732:
 
For quick reference, see also [http://cpp.operator-precedence.com this] equivalent, color-coded table.
 
=={{header|C sharp|C#}}==
[http://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/ MSDN documentation]
 
=={{header|Caché ObjectScript}}==
Line 708 ⟶ 852:
| 0 || Range separator || .. || Not a real operator, hardwired into syntax at specific points
|}
 
=={{header|dc}}==
As ''dc'' is a reverse-polish calculator, all operators/commands pop their arguments off the stack. They are simply evaluated in the order they appear in the code (from left to right).
 
=={{header|Delphi}}==
See table at [[#Free Pascal|Free Pascal]].
 
=={{header|Ecstasy}}==
 
Ecstasy's precedence table is based loosely on the C family of languages, but with significant tweaks to support the most common patterns without parenthesis. At a given level of precedence, all evaluation is from left to right.
 
{| class="wikitable"
! Order !! Operator !! Description !! Associativity
|-
| 1 || & || Unary reference-of, based on C syntax || Right-to-left
|-
| 2 || ++ || Post-increment || Left-to-right
|-
| || -- || Post-decrement ||
|-
| || () || Invoke method / call function ||
|-
| || [] || Array access ||
|-
| || ? || Postfix conditional ||
|-
| || . || Access object member ||
|-
| || .new || Postfix object creation ||
|-
| || .as || Postfix type assertion ||
|-
| || .is || Postfix type comparison ||
|-
| 3 || ++ || Pre-increment || Right-to-left
|-
| || -- || Pre-decrement ||
|-
| || + || Unary plus ||
|-
| || - || Unary minus ||
|-
| || ! || Logical NOT ||
|-
| || ~ || Bitwise NOT ||
|-
| 4 || ?: || Conditional "Elvis" || Right-to-left
|-
| 5 || * || Multiply || Left-to-right
|-
| || / || Divide ||
|-
| || % || Modulo ||
|-
| || / || Divide with remainder ||
|-
| 6 || + || Add || Left-to-right
|-
| || - || Subtract ||
|-
| 7 || << || Shift left || Left-to-right
|-
| || >> || Arithmetic shift right ||
|-
| || >>> || Logical shift right ||
|-
| || & || And ||
|-
| || ^ || Xor ||
|-
| || <nowiki>|</nowiki> || Or ||
|-
| 8 || .. || Range/intervale || Left-to-right
|-
| 9 || <- || Assignment || Right-to-left
|-
| 10 || < <= > >= || Relational || Left-to-right
|-
| || <=> || Order ||
|-
| 11 || == || Equality || Left-to-right
|-
| || != || Inequality ||
|-
| 12 || && || Conditional AND || Left-to-right
|-
| 13 || ^^ || Conditional XOR || Left-to-right
|-
| || <nowiki>||</nowiki> || Conditional OR ||
|-
| 14 || ? : || Conditional ternary || Right-to-left
|-
| 15 || : || Conditional ELSE || Right-to-left
|}
 
=={{header|Eiffel}}==
 
Line 847 ⟶ 1,084:
 
=={{header|Erlang}}==
Official documentation table: [[http://erlang.org/doc/reference_manual/expressions.html]], see "Operator Precedence" towards the end.
{| class="wikitable"
!Operators !! Associativity
|-
||<code>:</code> ||
|-
||<code>#</code> ||
|-
||(unary) <code>+</code>, (unary) <code>-</code>, <code>bnot</code>, <code>not</code> ||
|-
||<code>/</code>, <code>*</code>, <code>div</code>, <code>rem</code>, <code>band</code>, <code>and</code> || Left
|-
||<code>+</code>, <code>-</code>, <code>bor</code>, <code>bxor</code>, <code>bsl</code>, <code>bsr</code>, <code>or</code>, <code>xor</code> || Left
|-
||<code>++</code>, <code>--</code>||Right
|-
||<code>==</code>, <code>/=</code>, <code>=<</code>, <code><</code>, <code>>=</code>, <code>></code>, <code>=:=</code>, <code>=/=</code>||
|-
||<code>andalso</code>||
|-
||<code>orelse</code>||
|-
||<code>=</code>, <code>!</code>||Right
|-
||<code>catch</code>
|}
 
=={{header|F_Sharp|F#}}==
Line 895 ⟶ 1,157:
|| Start and End of an expression ||
|}
 
 
=={{header|FreeBASIC}}==
If operators have equal precedence, they then are evaluated in the order in of their associativity. The associativity may be Left-to-Right or Right-to-Left order.
 
As a rule, binary operators (such as +, ^) and unary postfix operators (such as (), ->) are evaluated Left-to-Right, and unary prefix operators (such as Not, @) are evaluated Right-to-Left.
 
Operators that have an associativity of "N/A" indicate that there is no expression in which the operator can be used where its order of operation would need to be checked, either by precedence or by associativity. Function-like operators such as Cast are always the first to be evaluated due to the parentheses required in their syntax. And assignment operators are always the last to be evaluated.
 
Parentheses can be used to override operator precedence. Operations within parentheses are performed before other operations
 
{| class="wikitable"
! Priority || Operator || Description || Associativity
|-
| ALIGN=CENTER|highest || CAST ||Type Conversion || N/A
|-
| ALIGN=CENTER|highest || PROCPTR ||Procedure pointer || N/A
|-
| ALIGN=CENTER|highest || STRPTR ||String pointer || N/A
|-
| ALIGN=CENTER|highest || VARPTR ||Variable pointer || N/A
|-
| ALIGN=CENTER|18 || [] ||String index || Left-to-Right
|-
| ALIGN=CENTER|18 || [] ||Pointer index || Left-to-Right
|-
| ALIGN=CENTER|18 || () ||Array index || Left-to-Right
|-
| ALIGN=CENTER|18 || () ||Pointer to member access || Left-to-Right
|-
| ALIGN=CENTER|18 || . ||Member access || Left-to-Right
|-
| ALIGN=CENTER|18 || -> ||Pointer to member access || Left-to-Right
|-
| ALIGN=CENTER|17 || @ ||Address of || Right-to-Left
|-
| ALIGN=CENTER|17 || * ||Value of || Right-to-Left
|-
| ALIGN=CENTER|17 || New ||Allocate Memory || Right-to-Left
|-
| ALIGN=CENTER|17 || Delete ||Deallocate Memory || Right-to-Left
|-
| ALIGN=CENTER|16 || ^ ||Exponentiate || Left-to-Right
|-
| ALIGN=CENTER|15 || - ||Negate || Right-to-Left
|-
| ALIGN=CENTER|14 || * ||Multiply || Left-to-Right
|-
| ALIGN=CENTER|14 || / ||Divide || Left-to-Right
|-
| ALIGN=CENTER|13 || \ ||Integer divide || Left-to-Right
|-
| ALIGN=CENTER|12 || MOD ||Modulus || Left-to-Right
|-
| ALIGN=CENTER|11 || SHL ||Shift left || Left-to-Right
|-
| ALIGN=CENTER|11 || SHR ||Shift right || Left-to-Right
|-
| ALIGN=CENTER|10 || + ||Add || Left-to-Right
|-
| ALIGN=CENTER|10 || - ||Subtract || Left-to-Right
|-
| ALIGN=CENTER| 9 || & ||String concatenation || Left-to-Right
|-
| ALIGN=CENTER| 8 || Is ||Run-time type information check || N/A
|-
| ALIGN=CENTER| 7 || = ||Equal || Left-to-Right
|-
| ALIGN=CENTER| 7 || <> ||Not equal || Left-to-Right
|-
| ALIGN=CENTER| 7 || < ||Less than || Left-to-Right
|-
| ALIGN=CENTER| 7 || <= ||Less than or equal || Left-to-Right
|-
| ALIGN=CENTER| 7 || >= ||Greater than or equal || Left-to-Right
|-
| ALIGN=CENTER| 7 || > ||Greater than || Left-to-Right
|-
| ALIGN=CENTER| 6 || NOT ||Complement || Right-to-Left
|-
| ALIGN=CENTER| 5 || AND ||Conjunction || Left-to-Right
|-
| ALIGN=CENTER| 4 || OR ||Inclusive Disjunction || Left-to-Right
|-
| ALIGN=CENTER| 3 || EQV ||Equivalence || Left-to-Right
|-
| ALIGN=CENTER| 3 || IMP ||Implication || Left-to-Right
|-
| ALIGN=CENTER| 3 || XOR ||Exclusive Disjunction || Left-to-Right
|-
| ALIGN=CENTER| 2 || ANDALSO ||Short Circuit Conjunction || Left-to-Right
|-
| ALIGN=CENTER| 2 || ORELSE ||Short Circuit Inclusive Disjunction || Left-to-Right
|-
| ALIGN=CENTER| 1 || =[>] ||Assignment || Left-to-Right
|-
| ALIGN=CENTER| 1 || &= ||Concatenate and Assign || N/A
|-
| ALIGN=CENTER| 1 || += ||Add and Assign || N/A
|-
| ALIGN=CENTER| 1 || -= ||Subtract and Assign || N/A
|-
| ALIGN=CENTER| 1 || *= ||Multiply and Assign || N/A
|-
| ALIGN=CENTER| 1 || /= ||Divide and Assign || N/A
|-
| ALIGN=CENTER| 1 || \= ||Integer Divide and Assign || N/A
|-
| ALIGN=CENTER| 1 || ^= ||Exponentiate and Assign || N/A
|-
| ALIGN=CENTER| 1 || MOD= ||Modulus and Assign || N/A
|-
| ALIGN=CENTER| 1 || AND= ||Conjunction and Assign || N/A
|-
| ALIGN=CENTER| 1 || EQV= ||Equivalence and Assign || N/A
|-
| ALIGN=CENTER| 1 || IMP= ||Implication and Assign || N/A
|-
| ALIGN=CENTER| 1 || OR= ||Inclusive Disjunction and Assign || N/A
|-
| ALIGN=CENTER| 1 || XOR= ||Exclusive Disjunction and Assign || N/A
|-
| ALIGN=CENTER| 1 || SHL= ||Shift Left and Assign || N/A
|-
| ALIGN=CENTER| 1 || SHR= ||Shift Right and Assign || N/A
|-
| ALIGN=CENTER| 1 || LET ||Assignment || N/A
|-
| ALIGN=CENTER|lowest || LET() ||Assignment || N/A
|}
 
=={{header|Free Pascal}}==
{| class="wikitable" style="margin: auto;"
|+ operator precedence in FreePascal
! operator !! precedence !! category
|-
| <code>not</code>, unary&nbsp;<code>+</code>, unary&nbsp;<code>-</code>, <code>@</code>, <code>**</code> || highest (first) || unary operators, power
|-
| <code>*</code>, <code>/</code>, <code>div</code>, <code>mod</code>, <code>and</code>, <code>shl</code>, <code>shr</code>, <code>as</code>, <code><<</code>, <code>>></code>, <strike><code>is</code> [until FPC 3.3.1]</strike>
| second
| multiplying operators
|-
| <code>+</code>, <code>-</code>, <code>or</code>, <code>xor</code>, <code>><</code>
| third
| adding operators
|-
| <code>=</code>, <code><></code>, <code><</code>, <code>></code>, <code><=</code>, <code>>=</code>, <code>in</code>, <code>is</code> [since FPC 3.3.1]
| lowest
| relational operators
|}
 
“[B]inary operators of the same precedence are left-associative.”
Nevertheless, “[t]he order in which expressions of the same precedence are evaluated is not guaranteed to be left-to-right.”
(quotes from the Free Pascal Reference Guide)
 
The default configuration sets <code>{$boolEval off}</code> (<code>{$B-}</code> for short).
This enables “lazy evaluation” and thus affects evaluation order.
 
One notable difference to standardized [[#Pascal|Pascal]] in operator precedence can be switched:
Normally, Free Pascal’s unary minus is at the highest precedence level.
With <code>{$modeSwitch ISOUnaryMinus+}</code>, which is enabled by default in <code>{$mode ISO}</code> and <code>{$mode extendedPascal}</code>, unary minus can be put at the same level as all other adding operators (like the ISO standards define).
 
=={{header|Furor}}==
Line 974 ⟶ 1,397:
! style="text-align: left" | Associativity
|-
! 910
<small>highest</small>
| <code>f x</code>
| Function application
| Left
|-
! 9
| <code>.</code>
| Function composition
Line 981 ⟶ 1,409:
|-
! 8
| <code>^, ^^, **</code>
| Power
| Right
|-
! 7
| <code>*, /, `quot`, `rem`, `div`, `mod`</code>
|
| Left
|-
! 6
| <code>+, -</code>
|
| Left
|-
! 5
| <code>: ++</code>
| Append to list
| Right
|-
! 4
| <code>==, /=, &lt;, &lt;=, &gt;=, &gt; </code>
| Comparisons
| Compare-operators
|
|-
! 4
| <code>&lt;*&gt; &lt;$&gt; </code>
| Functor ops
| Left
|-
! 3
Line 1,016 ⟶ 1,449:
|-
! 1
| <code>&gt;&gt;, &gt;&gt;=</code>
| Monadic ops
|
| Left
|-
! 1
| <code>=&lt;&lt; &lt;&vert;&gt; </code>
|
| Right
|-
! 0
| <code>$, $!, `seq`</code>
|
| Right
Line 1,158 ⟶ 1,591:
Here's an informal treatment of the grammar:
 
Conjunctions require a left and right argument, either of which may be a noun or a verb. If one argument is omitted the conjunction is curried with the remaining argument, forming an adverb (if the right argument is omitted, the precedence of the conjunction is treated as lower than the precedence of a verb -- this might be thought of as a consequence of the "long left scope" of conjunctions extending as far as possible).
 
Adverbs require a single left argument, which may be a noun or a verb.
Line 1,282 ⟶ 1,715:
=={{header|Kotlin}}==
This is well-documented [https://kotlinlang.org/docs/reference/grammar.html on the Kotlin language website].
 
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
As with Common Lisp and Scheme, Lambdatalk uses s-expressions so there is no need for operator precedence.
Such an expression "1+2*3+4" is written {+ 1 {* 2 3} 4}
</syntaxhighlight>
 
=={{header|Lang}}==
<pre>
# Precedence | Operator | Associativity | Operator name
# ===========|====================|===============|==========================================
# 0 | (opr) | - | Grouping
# | opr1(opr2) | left-to-right | Function calls
# | opr1[opr2] | left-to-right | Get item
# | opr1?.[opr2] | left-to-right | Optional get item
# | opr1::opr2 | left-to-right | Member access [1]
# | opr1?::opr2 | left-to-right | Optional member access [1]
# | opr1->opr2 | left-to-right | Member access pointer [1]
# -----------|--------------------|---------------|------------------------------------------
# 1 | @opr | right-to-left | Length
# | ^opr | | Deep copy
# -----------|--------------------|---------------|------------------------------------------
# 2 | opr1 ** opr2 | right-to-left | Power [2]
# -----------|--------------------|---------------|------------------------------------------
# 3 | +opr | right-to-left | Positive
# | -opr | | Inverse
# | ~opr | | Bitwise not
# | +|opr | | Increment
# | ▲opr | | Increment [Alternative non-ascii symbol]
# | -|opr | | Decrement
# | ▼opr | | Decrement [Alternative non-ascii symbol]
# | !opr | | Not
# -----------|--------------------|---------------|------------------------------------------
# 4 | opr1 * opr2 | left-to-right | Multiplication
# | opr1 / opr2 | | Division
# | opr1 ~/ opr2 | | Truncation division
# | opr1 // opr2 | | Floor division
# | opr1 ^/ opr2 | | Ceil division
# | opr1 % opr2 | | Modulo
# -----------|--------------------|---------------|------------------------------------------
# 5 | opr1 ||| opr2 | left-to-right | Concat
# | opr1 + opr2 | | Addition
# | opr1 - opr2 | | Subtraction
# -----------|--------------------|---------------|------------------------------------------
# 6 | opr1 << opr2 | left-to-right | Left shift
# | opr1 >> opr2 | | Right shift
# | opr1 >>> opr2 | | Right zero shift
# -----------|--------------------|---------------|------------------------------------------
# 7 | opr1 & opr2 | left-to-right | Bitwise and
# -----------|--------------------|---------------|------------------------------------------
# 8 | opr1 ^ opr2 | left-to-right | Bitwsie xor
# -----------|--------------------|---------------|------------------------------------------
# 9 | opr1 | opr2 | left-to-right | Bitwise or
# -----------|--------------------|---------------|------------------------------------------
# 10 | opr1 <=> opr2 | left-to-right | Spaceship
# | opr1 ~~ opr2 | | Instance of
# | opr1 == opr2 | | Equals
# | opr1 != opr2 | | Not equals
# | opr1 =~ opr2 | | Matches
# | opr1 !=~ opr2 | | Not matches
# | opr1 === opr2 | | Strict equals
# | opr1 !== opr2 | | Strict not equals
# | opr1 < opr2 | | Less than
# | opr1 > opr2 | | Greater than
# | opr1 <= opr2 | | Less than or equals
# | opr1 >= opr2 | | Greater than or equals
# -----------|--------------------|---------------|------------------------------------------
# 11 | opr1 && opr2 | left-to-right | And
# -----------|--------------------|---------------|------------------------------------------
# 12 | opr1 || opr2 | left-to-right | Or
# -----------|--------------------|---------------|------------------------------------------
# 13 | opr1 ?: opr2 | left-to-right | Elvis
# | opr1 ?? opr2 | | Null coalescing
# -----------|--------------------|---------------|------------------------------------------
# 14 | opr1 ? opr2 : opr3 | right-to-left | Inline if
# -----------|--------------------|---------------|------------------------------------------
# 15 | opr1 , opr2 | left-to-right | Comma
# -----------|--------------------|---------------|------------------------------------------
# 16 | opr1 = opr2 | right-to-left | Normal assignment
# | opr1 = opr2 | | Translation
# | opr1 ::= opr2 | | Lvalue operation assignment
# | opr1 ?= opr2 | | Condition operator assignment
# | opr1 := opr2 | | Math operator assignment
# | opr1 $= opr2 | | Operation operator assignment
# | opr1 += opr2 | | Addition assignment
# | opr1 -= opr2 | | Subtraction assignment
# | opr1 *= opr2 | | Multiplication assignment
# | opr1 /= opr2 | | Division assignment
# | opr1 ~/= opr2 | | Truncation division assignment
# | opr1 //= opr2 | | Floor division assignment
# | opr1 ^/= opr2 | | Ceil division assignment
# | opr1 **= opr2 | | Power assignment
# | opr1 %= opr2 | | Modulo assignment
# | opr1 >>= opr2 | | Right shift assignment
# | opr1 >>>= opr2 | | Right zero shift assignment
# | opr1 <<= opr2 | | Left shift assignment
# | opr1 &= opr2 | | Bitwise and assignment
# | opr1 ^= opr2 | | Bitwise xor assignment
# | opr1 |= opr2 | | Bitwise or assignment
# | opr1 |||= opr2 | | Concat assignment
# | opr1 ?:= opr2 | | Elvis assignment
# | opr1 ??= opr2 | | Null coalescing assignment
#
# Footnotes:
# 1) The unary operators (@, ^, +, -, ~, +|, ▲, -|, ▼, and !) have a higher binding than the member access operators if they are on the right of the member access operator.
# 2) The unary operators (+, -, ~, +|, ▲, -|, ▼, and !) have a higher binding than the power operator if they are on the right of the power operator.
#
# Some operators have multiple meanings but they keep the same precedence and associativity
</pre>
 
=={{header|LIL}}==
Line 1,371 ⟶ 1,913:
# Binding is done at the call site; therefore, method lookup is syntactically part of the call
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Here is an outline:
{| class="wikitable"
Line 1,476 ⟶ 2,018:
=={{header|min}}==
min is a stack language that uses postfix notation, so for the most part, all operators have the same precedence. Sigils are a small exception. However, they de-sugar to postfix. For example, the following two lines are equivalent:
<langsyntaxhighlight lang="min">42 :my-var
42 "my-var" define</langsyntaxhighlight>
 
=={{header|Nim}}==
Line 1,486 ⟶ 2,028:
! Relevant character
|-
! 910 (highest)
|
| <code>$</code>, <code>^</code>
|-
! 89
| <code>*</code>, <code>/</code>, <code>div</code>, <code>mod</code>, <code>shl</code>, <code>shr</code>, <code>%</code>
| <code>*</code>, <code>%</code>, <code>\</code>, <code>/</code>
|-
! 78
| <code>+</code>, <code>-</code>
| <code>+</code>, <code>-</code>, <code>~</code>, <code>|</code>
|-
! 67
| <code>&</code>
| <code>&</code>
|-
! 56
| <code>..</code>
| <code>.</code>
|-
! 45
| <code>==</code>, <code><=</code>, <code><</code>, <code>>=</code>, <code>></code>, <code>!=</code>, <code>in</code>, <code>notin</code>, <code>is</code>, <code>isnot</code>, <code>not</code>, <code>of</code>, <code>as</code>
| <code>=</code>, <code><</code>, <code>></code>, <code>!</code>
|-
! 34
| <code>and</code>
|
|-
! 23
| <code>or</code>, <code>xor</code>
|
|-
! 12
|
| <code>@</code>, <code>:</code>, <code>?</code>
|-
! 1
! 0 (lowest)
| assignment operator (like <code>+=</code>, <code>*=</code>)
|
|-
! 0 (lowest)
| arrow like operator (like <code>-></code>, <code>=></code>)
|
|-
Line 1,529 ⟶ 2,075:
 
=={{header|OCaml}}==
[httphttps://camlocaml.inria.fr/pub/docsorg/manual-ocaml/expr.html#@manual.kwd32ss:precedence-and-associativity This table] contains the precedence and associativity of operators and other expression constructs in OCaml, including user-defined operators.
 
=={{header|Odin}}==
Unary operators have the highest precedence.
 
There are seven precedence levels for binary and ternary operators.
 
{| class="wikitable"
!Precedence !! Operator
|-
|| 7 || <code>*</code> <code>/</code> <code>%</code> <code>%%</code> <code>&</code> <code>&~</code> <code><<</code> <code>>></code>
|-
|| 6 || <code>+</code> <code>-</code> <code><nowiki>|</nowiki></code> <code>~</code> <code>in</code> <code>not_in</code>
|-
|| 5 || <code>==</code> <code>!=</code> <code><</code> <code>></code> <code><=</code> </code>>=</code>
|-
|| 4 || <code>&&</code>
|-
|| 3 || <code><nowiki>||</nowiki></code>
|-
|| 2 || <code>..=</code> <code>..<</code>
|-
|| 1 || <code>or_else</code> <code>?</code> <code>if</code> <code>when</code>
|}
Binary operators of the same precedence associate from left to right. For instance "x / y * z" is the same as "(x / y) * z"
=={{header|Oforth}}==
 
Line 1,607 ⟶ 2,176:
 
=={{header|Pascal}}==
Standard Pascal, as laid out in ISO standard 7185, recognizes four precedence levels.
This table contains the precedence and associativity of operators:
Extended Pascal (EP), defined in ISO standard 10206, has one additional level, [[Exponentiation with infix operators in (or operating on) the base#Pascal|exponentiating operators]].
{| class="wikitable"
The following is a consolidated table for both standards:
! Priority || Description || Operators || Associativity
 
{| class="wikitable" style="margin: auto;"
|+ operator precedence in Pascal
! class !! operators
|-
| highest || negation || ||
* <code>not</code>
|-
| exponentiating operators ||
| 1 || unary operators ||<code> not @ </code>|| left to right
* <code>**</code> (only in EP)
* <code>pow</code> (only in EP)
|-
| multiplying operators ||
| 2 || mult operators ||<code> * / div mod and shl shr as << >> </code>|| left to right
* <code>*</code>
* <code>/</code>
* <code>div</code>
* <code>mod</code>
* <code>and</code>
* <code>and_then</code> (only available in EP)
|-
| 3 || add adding operators ||<code> + - or xor </code>|| left to right
* <code>+</code>
* <code>-</code>
* <code>><</code> (only in EP)
* <code>or</code>
* <code>or_else</code> (only available in EP)
|-
| relational operators ||
| 4 || relations ||<code> = <> < > <= >= in is </code>|| left to right
* <code>=</code>
|-
* <code><></code>
| lowest || || ||
* <code><</code>
* <code>></code>
* <code><=</code>
* <code>>=</code>
* <code>in</code>
|}
“Sequences of two or more operators of the same precedence shall be left associative.”
'''Example'''::<br>
(quote from ISO standards)
The expression <code> ( a>n and b<n ) </code> is equivalent to :
 
<code> ( ( a > (n and b) ) < n ) </code> , so, it is invalid !<br>
However, unlike some other programming languages, operator precedence ''does not'' define evaluation order.
we must code:<br>
This is because Pascal does not permit “lazy evaluation”:
<code> (a>n) and (b<n)</code>
Except in the case of <code>and_then</code> and <code>or_else</code>, the ''evaluation order implementation-defined''.
Some compilers, for instance the FPC ([[#Free Pascal|Free Pascal]] Compiler), evaluate “more complex” subexpression first before evaluating “easy” subexpressions (rationale: avoid register spilling).
 
=={{header|Perl}}==
Line 1,633 ⟶ 2,227:
 
=={{header|Phix}}==
{{libheader|Phix/basics}}
 
<!--(phixonline) - p2js automatically inserts paranthesis where/when needed-->
{| class="wikitable"
! style="text-align: left" | Precedence
Line 1,658 ⟶ 2,253:
!
| &
|-
!
| && <nowiki>||</nowiki>
|-
!
Line 1,664 ⟶ 2,262:
!
| = !=
|-
!
| && <nowiki>||</nowiki>
|-
!
Line 1,674 ⟶ 2,269:
| { , , , }
|}
Parenthesis is required to mix and/or/xor in an expression, without said you get a compilation error because the compiler refuses to guess what you actually mean.
 
Perhaps surprisingly Phix does not rely on associativity, mainly because it has a power() function rather than an infix operator for that.<br> You could alternatively say that all the above operators bar the unary ops are left associative, within their own precedence level.
 
Phix has reference counting with copy-on-write semantics: technically parameters are always passed by reference, which can improve performance, however attempting to modify anything with a reference count greater than one performs an internal clone (at that level) which means it ''behaves'' as if everything were being passed by value.<br>
It also has automatic pass by reference for local variables such that (eg) <code>table = somefunc(table)</code> does ''not'' increase the reference count (in practice the calling code's local variable becomes unassigned over the call) and consequently makes in-situ modification possible, which can obviously also significantly benefit performance.<br>
Under "with js", aka "with javascript_semantics", any said internal clone triggers a runtime error, effectively prohibiting copy-on-write semantics and thus ensuring the code is compatible with JavaScript, and in fact also implicitly banning the usual pass-by-sharing semantics of JavaScript, except where covered by the automatic pbr handling.
 
=={{header|PHP}}==
Line 1,711 ⟶ 2,312:
| lowest || || ||
|}
 
=={{header|Plain English}}==
Plain English treats an expression if it contains any of the operators 'plus', 'minus', 'times', 'divided by', or 'then'. Arguments in expressions are passed in by reference. Expressions are evaluated from left to right, ignoring traditional precedence rules.
 
For example, when evaluating 2 + 3 * 4, Plain English will calculate (2 + 3) * 4, and not 2 + (3 * 4).
 
=={{header|PureBasic}}==
Line 1,820 ⟶ 2,426:
=={{header|Q}}==
Operators have equal precedence and expressions are evaluated from right to left.
<langsyntaxhighlight lang="q">q)3*2+1
9
q)(3*2)+1 / Brackets give the usual order of precedence
Line 1,826 ⟶ 2,432:
q)x:5
q)(x+5; x:20; x-5)
25 20 0</langsyntaxhighlight>
 
=={{header|Quackery}}==
{{trans|Factor}}
Because Quackery uses postfix notation and relies entirely on fixed-argument function composition, all operators have the same precedence. Think of using a calculator that uses reverse-polish notation. Instead of writing <tt>(3+5)*2</tt>, you'd write <tt>3 5 + 2 *</tt>. There is no need for parentheses to clarify the order of operations.
 
=={{header|Racket}}==
Racket uses S-expr for its syntax, operators and functions show no precedences as all code is written as:
<syntaxhighlight lang Racket="racket">(function arguments ...)</langsyntaxhighlight>
 
function being any function or operator (language or user defined) and arguments are the arguments passed to it.
Line 1,966 ⟶ 2,576:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
The next table present operators from higher precedence (Evaluated first) to lower precedence.
 
Line 1,988 ⟶ 2,598:
 
See 3+5*4 # prints 23
</syntaxhighlight>
</lang>
 
=={{header|Ruby}}==
Line 2,080 ⟶ 2,690:
the Seed7 Structured Syntax Description ([http://seed7.sourceforge.net/manual/syntax.htm S7SSD])
A S7SSD statement like
<langsyntaxhighlight lang="seed7">$ syntax expr: .(). + .() is -> 7;</langsyntaxhighlight>
specifies the syntax of the <code>+</code> operator.
The right arrow <code>-&gt;</code> describes the associativity:
Binding of operands from left to right. With <code>7</code> the priority
of the <code>+</code> operator is defined. The syntax pattern
<syntaxhighlight lang ="seed7">.(). + .()</langsyntaxhighlight>
is introduced and delimited with dots (<code>.</code>). Without dots the pattern is
<pre>() + ()</pre>
Line 2,145 ⟶ 2,755:
 
For example:
<langsyntaxhighlight lang="ruby">1+2 * 3+4 # means: (1+2) * (3+4)</langsyntaxhighlight>
 
See also the [https://trizen.gitbooks.io/sidef-lang/content/syntax_and_semantics/operator_precedence.html documentation] on the precedence of operators.
Line 2,179 ⟶ 2,789:
|}
Note: // is the Euclidean division
 
=={{header|Smalltalk}}==
Smalltalk does not have operators which are built in to the language.
<br>All operations are message sends to a receiver object (aka virtual function calls).
<br>Messages are one of 3 types:
<br> <I>unary message</I>: no argument, alphanumeric name (highest precedence; left to right evaluation)
<br> <I>binary message</I>: any combination of special characters (left to right evaluation)
<br> <I>keyword message</I>: one or more arguments, alphanumeric name with colons (lowest precedence)
 
Unary message examples:
<br><code> negated abs conjugated size isZero</code>
<br>Binary examples:
<br><code> + - * % <=> ===> ˜˜ ˜= = , ,,</code> (yes "," (comma) and ",," are possible!)
<br>Keyword examples:
<br><code> min: max: at: at:put: from:to:do: bitAnd: bitOr:</code>
 
Within unary and binary messages, evaluation is strictly left to right. Keyword messages must be parenthesized.
 
No table can be presented here: there are too many such operators, and they can also be freely added by the programmer (i.e. you may define your own "Number fooBar" or "Number +-+-+ arg" messages (if it makes any sense to you).
Math expressions are usually parenthesized for better readability (by beginners):
<syntaxhighlight lang="smalltalk">5 + b negated. "same as 5 + (b negated); unary > binary"
a abs - b sqrt "same as (a abs) - (b sqrt); unary > binary"
a bitAnd:1+a abs. "same as a bitAnd:(1+(a abs)); unary > binary > keyword"
(a bitAnd:1+a) bitOr:(b bitAnd:1+2 abs). "ditto"
 
"Beginners might be confused by:"
5 + a * b "same as (5 + a) * b; all binary; therefore left to right"</syntaxhighlight>
 
=={{header|Standard ML}}==
{| class="wikitable"
|-
! Precedence
! Operator
! Associativity
|-
! <small>highest</small>
|
|
|-
! 7
| <code>* / div mod</code>
| left
|-
! 6
| <code>+ - ^</code>
| left
|-
! 5
| <code>:: @</code>
| right
|-
! 4
| <code>= &lt;&gt; &gt; &gt;= &lt; &lt;=</code>
| left
|-
! 3
| <code>:= o</code>
| left
|-
! 0
| <code>before</code>
| left
|}
 
=={{header|Tcl}}==
162

edits