Operator Precedence and Associativity in Programming
Operator Precedence is a rule that determines the order in which operators are evaluated in an expression. It defines which operators take precedence over others when they are combined in the same expression. Operators with higher precedence are evaluated before operators with lower precedence. Parentheses can be used to override the default precedence and explicitly specify the order of evaluation.
Operator Associativity is a rule that determines the grouping of operators with the same precedence in an expression when they appear consecutively. It specifies the direction in which operators of equal precedence are evaluated. The two common associativities are:
- Left to Right (Left-Associative): Operators with left associativity are evaluated from left to right. For example, in the expression
a + b + c
, the addition operators have left associativity, so the expression is equivalent to(a + b) + c
. - Right to Left (Right-Associative): Operators with right associativity are evaluated from right to left. For example, in the expression
a = b = c
, the assignment operator=
has right associativity, so the expression is equivalent toa = (b = c)
.
Precedence | Operator | Description | Associativity |
---|---|---|---|
1 | () | Parentheses | Left-to-Right |
x++, x– | Postfix increment, decrement | ||
2 | ++x, –x | Prefix increment, decrement | Right-to-Left |
‘+’ , ‘-‘ | Unary plus, minus | ||
! , ~ | Logical NOT, Bitwise complement | ||
* | Dereference Operator | ||
& | Addressof Operator | ||
3 | *, /, % | Multiplication, division, modulus | Left-to-Right |
4 | +, – | Addition, subtraction | Left-to-Right |
5 | << , >> | Bitwise shift left, Bitwise shift right | Left-to-Right |
6 | < , <= | Relational less than, less than or equal to | Left-to-Right |
> , >= | Relational greater than, greater than or equal to | ||
7 | == , != | Relational is equal to, is not equal to | Left-to-Right |
8 | & | Bitwise AND | Left-to-Right |
9 | ^ | Bitwise XOR | Left-to-Right |
10 | | | Bitwise OR | Left-to-Right |
11 | && | Logical AND | Left-to-Right |
12 | || | Logical OR | Left-to-Right |
13 | ?: | Ternary conditional | Right-to-Left |
14 | = | Assignment | Right-to-Left |
+= , -= | Addition, subtraction assignment | ||
*= , /= | Multiplication, division assignment | ||
%= , &= | Modulus, bitwise AND assignment | ||
^= , |= | Bitwise exclusive, inclusive OR assignment | ||
<<=, >>= | Bitwise shift left, right assignment | ||
15 | , | comma (expression separator) | Left-to-Right |
What are Operators in Programming?
Operators in programming are essential symbols that perform operations on variables and values, enabling tasks like arithmetic calculations, logical comparisons, and bitwise manipulations. In this article, we will learn about the basics of operators and their types.
Table of Content
- What are Operators in Programming?
- Types of Operators in Programming
- Operator Precedence and Associativity in Programming
- Frequently Asked Questions (FAQs) related to Programming Operators
Contact Us