Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 291
  • Last Modified:

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?
0
shahrine99
Asked:
shahrine99
  • 3
  • 2
  • 2
  • +2
3 Solutions
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now