 # Precedence Rules for Arithmetic Operators

Operator/ConventionDescriptionExplanation
( )Items within parentheses are evaluated firstIn 2 * (x + 1), the x + 1 is evaluated first, with the result then multiplied by 2
unary -- used for negation (unary minus) is nextIn 2 * -x, the -x is computed first, with the result then multiplied by 2
* / %Next to be evaluated are *, /, and %, having equal precedence% is not described in this table
+ -Finally come + and - with equal precedenceIn y = 3 + 2 * x, the 2 * x is evaluated first, with the result then added to 3, because * has higher precedence than +. Spacing doesn't matter: y = 3+2 * x would still evaluate 2 * x first.
left-to-rightIf more than one operator of equal precedence could be evaluated, evaluation occurs left to rightIn y = x * 2 / 3, the x * 2 is first evaluated, with the result then divided by 3

Precedence rules for arithmetic, logical, and relational operators.
Operator/Convention Description Explanation
( ) Items within parentheses are evaluated first In (a * (b + c)) – d, the + is evaluated first, then *, then -.
! ! (logical NOT) is next ! x || y is evaluated as (!x) || y
* / % + – Arithmetic operators (using their precedence rules; see earlier section) z – 45 * y < 53 evaluates * first, then -, then <.
< <= > >= Relational operators x < 2 || x >= 10 is evaluated as (x < 2) || (x >= 10) because < and >= have precedence over ||.
== != Equality and inequality operators x == 0 && x >= 10 is evaluated as (x == 0) && (x >= 10) because < and >= have precedence over &&.
== and != have the same precedence and are evaluated left to right.
&& Logical AND x == 5 || y == 10 && z != 10 is evaluated as (x == 5) || ((y == 10) && (z != 10)) because && has precedence over ||.
|| Logical OR

Did you enjoy this content?