Operators and Expressions II-45
Table 3.7 Assignment Operators
=
*= /= %=
+=
– =
<<= >>=
17. What are compound assignment operators?
Ans: A compound assignment operator consists of a binary operator and assignment operator.
They carry out binary operation and assign the result to the left operand.
Compound assignment operators and equivalent expressions are as follows:
a += b is equivalent to a = a + b
a −= b is equivalent to a = a − b
a *= b is equivalent to a = a * b
a /= b is equivalent to a = a/b
a %= b is equivalent to a = a%b
a <<= b is equivalent to a = a << b
a >>= b is equivalent to a = a >> b
a &= b is equivalent to a = a&b
a |= b is equivalent to a = a|b
a ^= b is equivalent to a = a^b
18. Explain precedence and associativity of operators.
Ans: Operators have the following properties:
1. Precedence: It refers to the priority given to the operator while evaluation of an expression.
When an expression contains many operators, the operations are carried out according to the
priority of the operators. The higher precedence operations are evaluated first.
2. Associativity: When an expression has operators with equal precedence, the associativity property
decides which operation to be carried out first. Associativity means the direction of execution.
It has two types.
(a) Left to right: In this type, the expression evaluation starts from the left to the right direction.
(b) Right to left: In this type, the expression evaluation starts from the right to the left direction.
19. What are the differences between precedence and associativity?
Ans: Precedence decides which operator to execute first when there are more than one different
operators with different priorities. On the other hand, associativity decides which operator to execute
first when there is more than one operator with same priority.
20. What is the difference between pre- and post-increments?
Ans: Consider the following pre- and post-increment operators:
1. Pre-increment example ++a;
2. Post-increment example a++;
In the first case, the variable a’s value is incremented first before being used and in the second case value
of a is incremented after being used.
M03_ITL-ESL4791_02_SE_C03.indd 45 12/22/2012 5:00:07 PM