0
Question About a+=b In C++
Why does "a+=b" mean "a=a+b" and not "a+b=b"? What I had in mind was kind of like the distributive property in maths except with operations instead of numbers but I don't think that works. Or is it like distributive property where a+b is combined with a=b, resulting in a+b=b and since it's the first version of b because of a=b, it's a+b=a? So basically is there logic to it or is it just that way?
4 Respostas
+ 1
Beacuse in c++ the "=" is an assignment operator and it assigns a value form the right to the left.
So, if a=0 and b=1 then a+=b the value of a becomes 1(0+1)
again with a=a+b you are adding a+b and storing it in a.
But a+b=b is wrong because you can't assign the value of b to both a+b
The assignment operator "=" assigns valus from right to left
Hope this helps
+ 9
The assignment operator '=' assigns RHS value to LHS value, not the other way around. Thinking otherwise would only confuse you.
According to the definition provided, a + b = b makes no sense.
a = a + b, on the other hand, assigns the sum of the previous value of a and b to a, so a would hold the value of (a+b).
+ 1
Thank you both I understand now.
+ 1
Often times in programming languages each symbols have differnt meanings than that which are used in everyday life. people often get confused with "=" "==" and "==="
Glad you understood it pretty quickly.