Thread: += and -=

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    5

    += and -=

    I've come across += and -= and have no clue as to what they do, and I cant find any tutorials either. Can anyone explain?

    EDIT: I've also noticed some code..

    Code:
    if (i)
    { etc etc etc }
    How can 'i' be a condition?
    Last edited by Sn0mAN; 07-11-2007 at 07:17 PM.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    • L += R;

      Equivalent to:

      Code:
      L = L + R;
    • L -= R;

      Equivalent to:

      Code:
      L = L - R;

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by Sn0mAN View Post
    I've come across += and -= and have no clue as to what they do, and I cant find any tutorials either. Can anyone explain?

    EDIT: I've also noticed some code..

    Code:
    if (i)
    { etc etc etc }
    How can 'i' be a condition?
    In C, everything non-zero is true (ie 0 is false). So if i is not 0, etc etc etc will be executed.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The operator != itself returns 1 or 0, so when you use
    Code:
    if(x != 0)
    it's like you're using
    Code:
    if( (x != 0) != 0)
    or
    Code:
    if ( (x != 0) != 0) != 0)
    ad infinitum. So you can just leave out the first != 0 and go
    Code:
    if(x)
    Every comparison operator (indeed, every operator) returns a zero or non-zero value. When you use if(x != 0), you're just using the zero or non-zero value value of x != 0. You can also use the zero or non-zero value of a variable. You could go
    Code:
    int x = 1, y = x != 0;
    if(y)
    or even
    Code:
    if(x)
    instead of
    Code:
    if(x != 0)
    . . . okay, so that wasn't a very good explanation. But I hope you know what I mean.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    or
    Code:

    if ( (x != 0) != 0) != 0)

    ad infinitum. So you can just leave out the first != 0 and go
    Code:

    if(x)
    lol... Funny way of putting it

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I know, it's definitely a novel idea.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    With += and -= (or any of the compound assignment operators), the left-hand side is only evaluated once, instead of twice with the expanded form, so if its evaluation has a side effect, they're not equivalent (although this is uncommon).
    Example:

    http://books.google.com/books?id=svQ...nidOjt7qKR7yl8

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed