Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Order of operations
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Programming languages== Most [[programming languages]] use precedence levels that conform to the order commonly used in mathematics,{{r|Henderson}} though others, such as [[APL (programming language)|APL]], [[Smalltalk]], [[Occam (programming language)|Occam]] and [[Mary (programming language)|Mary]], have no [[Operator (programming)|operator]] precedence rules (in APL, evaluation is strictly right to left; in Smalltalk, it is strictly left to right). Furthermore, because many operators are not associative, the order within any single level is usually defined by grouping left to right so that <code>16/4/4</code> is interpreted as {{nowrap|(16/4)/4 {{=}} 1}} rather than {{nowrap|16/(4/4) {{=}} 16}}; such operators are referred to as "left associative". Exceptions exist; for example, languages with operators corresponding to the [[cons]] operation on lists usually make them group right to left ("right associative"), e.g. in [[Haskell (programming language)|Haskell]], <code>1:2:3:4:[] == 1:(2:(3:(4:[]))) == [1,2,3,4]</code>. [[Dennis Ritchie]], creator of the [[C (programming language)|C language]], said of the precedence in C (shared by programming languages that borrow those rules from C, for example, [[C++]], [[Perl]] and [[PHP]]) that it would have been preferable to move the [[Bitwise operation|bitwise operators]] above the [[Relational operator|comparison operators]].{{r|Ritchie}} Many programmers have become accustomed to this order, but more recent popular languages like [[Python (programming language)|Python]]{{r|Python}} and [[Ruby (programming language)|Ruby]]{{r|Ruby}} do have this order reversed. The relative precedence levels of operators found in many C-style languages are as follows: {| class="wikitable" |1 || <code>()</code> <code>[]</code> <code>-></code> <code>.</code> <code>::</code> || Function call, scope, array/member access |- |2 || <code>!</code> <code>~</code> <code>-</code> <code>+</code> <code>*</code> <code>&</code> <u>[[sizeof|<code>sizeof</code>]]</u> ''[[Type conversion|type cast]]'' <code>++</code> <code>--</code> || (most) unary operators, [[sizeof]] and [[Type conversion|type casts]] (right to left) |- |3 || <code>*</code> <code>/</code> <code>%</code> <code>MOD</code> || Multiplication, division, [[modular arithmetic|modulo]] |- |4 || <code>+</code> <code>-</code> || Addition and subtraction |- |5 || <code><<</code> <code>>></code> || Bitwise shift left and right |- |6 || <code><</code> <code><=</code> <code>></code> <code>>=</code> || Comparisons: less-than and greater-than |- |7 || <code>==</code> <code>!=</code> || Comparisons: equal and not equal |- |8 || <code>&</code> || Bitwise AND |- |9 || <code>^</code> || Bitwise exclusive OR (XOR) |- |10 || <code><nowiki>|</nowiki></code> || Bitwise inclusive (normal) OR |- |11 || <code>&&</code> || Logical AND |- |12 || <code><nowiki>||</nowiki></code> || Logical OR |- |13 || <code>?</code> <code>:</code> || Conditional expression (ternary) |- |14 || <code>=</code> <code>+=</code> <code>-=</code> <code>*=</code> <code>/=</code> <code>%=</code> <code>&=</code> <code><nowiki>|=</nowiki></code> <code>^=</code> <code><<=</code> <code>>>=</code> || Assignment operators (right to left) |- |15 || <code>,</code> || [[Comma operator]] |} [[File:Algol grammar expr svg.svg|thumb|500px|Simplified [[Context-free grammar|formal grammar]] for arithmetical expressions in a programming language ''(left)'',{{r|Algol}} and derivation of the example expression <code>(a+b)^2/2</code> ''(right)''. The latter corresponds to a hierarchical structure ("[[abstract syntax tree|syntax tree]]") which is unique for the given expression. The [[compiler]] generates [[machine code]] from the tree in such a way that operations originating at the lowest hierarchy level are executed first.]]Examples: * <code>!A + !B</code> is interpreted as <code>(!A) + (!B)</code> * <code>++A + !B</code> is interpreted as <code>(++A) + (!B)</code> * <code>A + B * C</code> is interpreted as <code>A + (B * C)</code> * <code>A || B && C</code> is interpreted as <code>A || (B && C)</code> * <code>A && B == C</code> is interpreted as <code>A && (B == C)</code> * <code>A & B == C</code> is interpreted as <code>A & (B == C)</code> (In [[Python (programming language)|Python]], [[Ruby (programming language)|Ruby]], [[PARI/GP]] and other popular languages, <code>A & B == C</code> is interpreted as <code>(A & B) == C</code>.) [[Source-to-source compiler]]s that compile to multiple languages need to explicitly deal with the issue of different order of operations across languages. [[Haxe]] for example standardizes the order and enforces it by inserting brackets where it is appropriate. The accuracy of software developer knowledge about binary operator precedence has been found to closely follow their frequency of occurrence in source code.{{r|Jones}}
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)