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

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

1. ### malliGuest

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

2. ### David ShinGuest

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

3. ### Pete BeckerGuest

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
4. ### Mike WahlerGuest

"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
5. ### KevGuest

"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
6. ### Pete BeckerGuest

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
7. ### Marcus KwokGuest

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
8. ### TomHanksGuest

> 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

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