compound assigment operators

I have a question regarding to one of the questions on my homework. The question is:

C, C++, and Java contain compound assignment operators such as +=. In most cases, writing <var> += <expr> is equivalent to writing <var> = <var> + <expr>. Give an example in which the two assignments are not equivalent.

I cannot think of an example. Can you please give me a hint?
shahrine99Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dbkrugerCommented:
In C++, you can overload operators to do what you like. But you can't in C and Java, so I suspect that is not what they mean.

I think it's fairly likely the question is itself in error. Some people mistakenly think that x = x + 1 is two operations, while x += 1 is more effiicient, or atomic (indivisible) etc. But it's not true. It could be true, on any particular (bad) compiler.

Another example I've seen of unsophisticated teachers getting too clever for themselves is in recommending the use of for loops because they generate "more efficient" code, when in fact, any compiler in which they are different at all is written by poor programmers, since it represents double work.

I'll be interested to see if anyone else has an interesting subtle case I'm not thinking of.....

Oh, by the way, the reverse case is very interesting. With floating point:

a = x + y - z

can yield different answers depending on the machine (of course, different roundoff properties), but more surprising, on the compiler (different sequence of instructions or choice of which instructions, if using high speed vectorizing instructions (reduced accuracy), debugging vs. optimized (in vs. out of register). It is conceivable that this is what is meant, though I think it perverts the lesson. It's not that the two forms are different, it's that doing anything differently in floating point will result in a different answer.

0
efnCommented:
According to the C++ standard, the only difference is in how many times <var> is evaluated.  If <var> is an expression (or a macro), that might make a difference.  If it's just a variable name, I don't see how it could.  (This is all aside from the operator overloading that dbkruger mentioned.)
0
WelkinMazeCommented:
Hi,
I suppose that if
<var> = a
<expr> = i + ++i

and brackets are not used, then the two assignments won't be equivalent, since they are:

a =+ i + ++i ;
a = a + i + ++i;
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

rahul_r_jadhavCommented:
http://www.cppreference.com/operator_precedence.html mentions all the operators.
Following are the all operators of var1 op = var2

+=
-=
*=
/=
%=
&=
^=
|=
<<=
>>=

atleast these operators work same as var1 = var1 op var2.
logical operators are binary but can not be used like var1 op = var2.

e.g

var1 &&= var2; is not valid statement, gives compilation error.
where as var1 = var1 && var2; is possible.

so by any chance are you looking for this??
0
rbvoigtCommented:
As efn said, the difference will be when the l-value has side effects.

Example:
int a[10];
int *p = a;


*(p++) += 5;
is not the same as
*(p++) = *(p++) + 5;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WelkinMazeCommented:
rbvoigt, according to <var> += <expr> the left side seems to be a variable, only the right side seems to be an expression.
0
rbvoigtCommented:
Well, often "<var>" is used to mean "an expression resolving to a variable, i.e. an l-value".

Anyway, WelkinMaze is close because he suggests operator precedence, but his example is way out.

You need to parenthesize the expression...

a *= x + 5;
means
a = a * (x + 5);
not
a = a * x + 5;
0
WelkinMazeCommented:
Exactly! Thus the reason why I give the example above. It is just as it has to be in order to do the trick. And I'm pretty sure that this is the wanted one. At least nobody has suggested something else till now and I also cannot think of something else also.
0
dbkrugerCommented:
I missed that expr bit.
Yes, side effects are the difference.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.