2 Replies - 533 Views - Last Post: 11 February 2014 - 01:51 PM Rate Topic: -----

#1 Evil Overlord  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-February 14

Compound assignment operators in C++

Posted 11 February 2014 - 11:35 AM

Greetings. I am a C++ student and I have a simple question I'd like to ask about increase and decrease assignment operators (++,--).

I've been told that these three statements are equivalent:
a++;  //maximum compression
a+=1;
a=a+1;  //minimum compression


Now let's take a look at the situation I've got:

This is a couple of examples where variable "b" is compressed by using an increase assignment operator.

Example 1:
a=++b;


Example 2:
a=b++;


How would they look like presented as simple assignments? My hypothesis is that both examples would look like this:
a=(b=b+1)


Am I missing something important here? I got the feeling something's wrong.
Please help if you can. Thanks.

I apologize in advance in case information is missing, or I'm posting something inadequate.

Is This A Good Question/Topic? 0
  • +

Replies To: Compound assignment operators in C++

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4142
  • View blog
  • Posts: 12,892
  • Joined: 25-December 09

Re: Compound assignment operators in C++

Posted 11 February 2014 - 11:51 AM

Example 1 would be b = b + 1; a = b;
You would do the increment of b before you assign the value of b to a.

Example 2 would be a = b; b = b + 1;
You would do the assignment of the value of b to a before you increment the value of b.

Jim
Was This Post Helpful? 1
  • +
  • -

#3 Evil Overlord  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-February 14

Re: Compound assignment operators in C++

Posted 11 February 2014 - 01:51 PM

Thanks, @jimblumberg, you made my day. ^^
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1