A - Which of the following arithmetic expressions is identical to the expression a += b * c
?
a = (a + b) * c
a = b * c
a = a + b * c
a = ++b * c
B - Which of these following expressions is wrong ?
c=a+b
a+b=c
a=b+c
c+=a+b
Apparently the answers are A = 3 and B = 2, would someone please explain how these are the answers. I believe these questions are related to C#.
A. x += y
is just shorthand for x = x + y
, the compiler always expands it out as such
https://msdn.microsoft.com/en-us/library/sa7629ew.aspx
B. C# isn't like math where you can rearrange an equation as you wish. As such, you can't have an expression such as a+b
on the lefthand side of an assignment, which is what the =
does. How would you store the value of c
in a+b
? it just isn't logical so this is the clear wrong answer. In programming, =
does not have the same meaning as an equals sign in a mathematical equation.
https://msdn.microsoft.com/en-us/library/sbkb459w.aspx