why only var1+=var2,why not var1=+var2

Discussion in 'C++' started by malli, Oct 21, 2005.

  1. malli

    malli Guest

    hi i am very new to c and c++
    In my few days experience i came to know that we should use
    incrementing operator as follows
    var1+=var2
    var1-=var2
    var1*=var2
    var1/=var2
    why don't we use the above assignment statements as follows.I mean
    why don't the developers of c & c++ used the following notations as
    incrementing and decrementing operators
    var1=+var2
    var1=-var2
    var1=*var2
    var1=/var2

    Please tell me the correct reason.I will be so thankful to u.
    Advance thanks .
    malli, Oct 21, 2005
    #1
    1. Advertising

  2. malli

    David Shin Guest

    It's the syntax.

    Why do we use "+" for addition and "-" for subtraction?
    Why not "*" for addition and "/" for subtraction?
    David Shin, Oct 21, 2005
    #2
    1. Advertising

  3. malli

    Pete Becker Guest

    That used to be the way it was done, but it's too easy to make mistakes:

    var1 =+1;
    var1 = +1;
    var1 =-1;
    var1 = -1;

    When the equal sign comes after the operation, if you accidentally add a
    space in the middle it becomes invalid instead of changing meaning.

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
    Pete Becker, Oct 21, 2005
    #3
  4. malli

    Mike Wahler Guest

    "malli" <> wrote in message
    news:...
    > hi i am very new to c and c++
    > In my few days experience i came to know that we should use
    > incrementing operator as follows
    > var1+=var2
    > var1-=var2
    > var1*=var2
    > var1/=var2


    Those are not increment operators. The increment operator
    is ++

    > why don't we use the above assignment statements as follows.I mean
    > why don't the developers of c & c++ used the following notations as
    > incrementing and decrementing operators
    > var1=+var2
    > var1=-var2
    > var1=*var2
    > var1=/var2


    Why should we? Do you feel there's some advantage to that,
    or that those are somehow superior to the ones we have?

    >
    > Please tell me the correct reason.


    Because the language designers chose that way (a very important
    reason is because that's the way it's done in C -- and one of
    the design goals of C++ was to maintain as much compatibility
    with C as possible.)

    -Mike
    Mike Wahler, Oct 21, 2005
    #4
  5. malli

    Kev Guest

    "malli" <> wrote in news:1129918348.131683.315850
    @g14g2000cwa.googlegroups.com:

    > var1=-var2


    what if there was a little typo and it was

    var1= -var2 ?
    Kev, Oct 21, 2005
    #5
  6. malli

    Pete Becker Guest

    Pete Becker wrote:
    > That used to be the way it was done,


    that is, that's how it was done in the ancient days of early C. C
    removed this long before C++ came along.


    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
    Pete Becker, Oct 21, 2005
    #6
  7. malli

    Marcus Kwok Guest

    malli <> wrote:
    > hi i am very new to c and c++
    > In my few days experience i came to know that we should use
    > incrementing operator as follows
    > var1+=var2
    > var1-=var2
    > var1*=var2
    > var1/=var2
    > why don't we use the above assignment statements as follows.I mean
    > why don't the developers of c & c++ used the following notations as
    > incrementing and decrementing operators
    > var1=+var2
    > var1=-var2
    > var1=*var2
    > var1=/var2
    >
    > Please tell me the correct reason.I will be so thankful to u.
    > Advance thanks .


    IIRC, old C allowed you to do this. However, there is an ambiguity:

    var1=-var2

    could mean either

    var1 = var1 - var2

    or

    var1 = -var2

    In order to avoid the ambiguity, they removed the =- form.

    --
    Marcus Kwok
    Marcus Kwok, Oct 21, 2005
    #7
  8. malli

    TomHanks Guest

    > var1=-var2

    Yes. And also,
    if I have something like:
    int n;
    n=-1;
    Now, it will not be clear if I want to assign -1 here or wanted to have
    it as: n = n-1
    TomHanks, Oct 21, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christopher M. Lusardi
    Replies:
    1
    Views:
    419
    Victor Bazarov
    Nov 19, 2004
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,840
    Smokey Grindel
    Dec 2, 2006
  3. Christopher M. Lusardi
    Replies:
    1
    Views:
    343
    Victor Bazarov
    Nov 19, 2004
  4. Replies:
    17
    Views:
    383
  5. Luca Scaljery
    Replies:
    5
    Views:
    99
    Patrick Spence
    Feb 14, 2007
Loading...

Share This Page