Precedence and Associativity of All Operators

This tutorial provides a reference to all the operators that have been discussed in previous tutorials, showing the different operators in a single place with their precedence and associativity. At the end, practice questions are also provided, so don’t forget to solve them.


Precedence and Associativity of All Operators

PrecedenceOperatorsAssociativity
1st() Left to right
2ndPostfix increment and decrement (++, –)Left to right
3rdUnary + and -, Logical negation (!), Prefix increment and decrement (++, –)Right to left
4th*, /, %Left to right
5th+, – Left to right
6th<, <=, >, >=Left to right
7th==, !=Left to right
8th&&Left to right
9th||Left to right
10th=, +=, -=, *=, /=, %=, <<=, >>=, &=, |=, ^=Right to left
11th, (Comma)Left to right

Practice Questions

Question 1

Consider the variables a = 5, b = 3, and c = 7. Solve this expression a + b * c - b / a

I have solved the question. Show me the solution.
I haven’t solved the question. It’s too difficult for me. I want to see your solution.
explanation
a + b * c - b / a
= 5 + 3 * 7 - 3 / 5
= 5 + 21 - 0 (integer division)
= 26
Question 2

Consider the variables x = 10, y = 5, and z = 8. Solve this expression x % y + z * x - y

I have solved the question. Show me the solution.
I haven’t solved the question. It’s too difficult for me. I want to see your solution.
explanation
x % y + z * x - y
= 10 % 5 + 8 * 10 - 5
= 0 + 80 - 5
= 75
Question 3

Consider the variables a = 5, b = 3, c = 7, d = 2. Solve this expression result = (a += b) * (c > d) + (++b, --a) && (c <= d)

I have solved the question. Show me the solution.
I haven’t solved the question. It’s too difficult for me. I want to see your solution.
explanation
result = (8) * (c > d) + (++b, --a) && (c <= d)
result = (8) * (1) + (++b, --a) && (c <= d)
result = (8) * (1) + (3, 7) && (c <= d)
result = (8) * (1) + (7) && (c <= d) 
result = (8) * (1) + (7) && (0)
result = 8 + (7) && (0)
result = 15 && 0
result = 0
Question 4

Consider the variables p = 15, q = 4, r = 3, s = 2. Solve this expression (p %= q) + (r != s) && (q += r, p -= s) || (r *= q)

I have solved the question. Show me the solution.
I haven’t solved the question. It’s too difficult for me. I want to see your solution.
explanation
(p %= q) + (r != s) && (q += r, p-=s) || (r *= q)
(3) + (r != s) && (q += r, p-=s) || (r *= q)   [p = p % q => p = 15 % 4 => p = 3]
(3) + (1) && (q += r, p-=s) || (r *= q)   [3 != 2 = True = 1]
(3) + (1) && (7, 1) || (r *= q)   [q = 7, p = 3 - 2 = 1]
(3) + (1) && (7, 1) || (21)   [r = r * q => r = 3 * 7 = 21]
4 && (7, 1) || (21)
4 && 1 || 21   [(7, 1) = 1]
1 || 21
1
Question 5

Consider the variables m = 6, n = 3, o = 4, p = 2. Solve this expression (m *= n) / (o < p) - (n++, m--, o += p)

I have solved the question. Show me the solution.
I haven’t solved the question. It’s too difficult for me. I want to see your solution.
explanation
(m *= n) / (o < p) - (n++, m--, o += p)
(18) / (o < p) - (n++, m--, o += p)   [m = m * n = 6 * 3 = 18]
(18) / (0) - (n++, m--, o += p)   [o < p = 4 < 2 = False = 0]
(18) / (0) - (4, 17, 6)   [n++ => n = 4, m-- => m = 17, o += p => o = 4 + 2 = 6]
(18) / (0) - (6)   [(4, 17, 6) = 6]
undefined   [because of division by zero]


Leave a comment

Leave a comment

Your email address will not be published. Required fields are marked *

Thank you for choosing to leave a comment. Please be aware that all comments are moderated in accordance with our policy. For more information, please review our comments policy. Rest assured that your email address will not be shared with anyone. Kindly refrain from using keywords in your comment. Let’s maintain a respectful atmosphere and engage in meaningful conversations.