compound assigment operators

Posted on 2006-04-25
Last Modified: 2008-02-01
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?
Question by:shahrine99
    LVL 11

    Expert Comment

    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.

    LVL 15

    Expert Comment

    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.)
    LVL 11

    Assisted Solution

    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;
    LVL 2

    Assisted Solution

    by:rahul_r_jadhav 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.


    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??
    LVL 1

    Accepted Solution

    As efn said, the difference will be when the l-value has side effects.

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

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

    Expert Comment

    rbvoigt, according to <var> += <expr> the left side seems to be a variable, only the right side seems to be an expression.
    LVL 1

    Expert Comment

    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;
    a = a * (x + 5);
    a = a * x + 5;
    LVL 11

    Expert Comment

    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.
    LVL 11

    Expert Comment

    I missed that expr bit.
    Yes, side effects are the difference.

    Featured Post

    Gigs: Get Your Project Delivered by an Expert

    Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

    Join & Write a Comment

      Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now