Question

  • Thread starter Thread starter gswin
  • Start date Start date
G

gswin

What is the difference between the following:

a. x = 2% 2 + 2 * 2 - 2/2

b. x = (2 % 2 + 2) * 2 - 2/2

Thnx.
 
a. The value of x will be:
x = (2%2) + (2*2) - (2/2)
= 0 + 4 - 1
= 3
b. The value of x in this case will be:
x = ((2%2) + 2)*2 - (2/2)
= (0+2)*2 - (2/2)
= 4-1
= 3.

Though the values of x in the above cases are same, there is a difference in
the order/precedence in which the expressions are evaluated. Try with
different numbers like
1%2+3*4-5/2 and (1%2+3)*4 - 5/2, where the values differ.
Refer: Operator precedence

Thanks
VijayaKrishna P.
 
THANK YOU!
-----Original Message-----
a. The value of x will be:
x = (2%2) + (2*2) - (2/2)
= 0 + 4 - 1
= 3
b. The value of x in this case will be:
x = ((2%2) + 2)*2 - (2/2)
= (0+2)*2 - (2/2)
= 4-1
= 3.

Though the values of x in the above cases are same, there is a difference in
the order/precedence in which the expressions are evaluated. Try with
different numbers like
1%2+3*4-5/2 and (1%2+3)*4 - 5/2, where the values differ.
Refer: Operator precedence

Thanks
VijayaKrishna P.



.
 
Back
Top