Velocity Reviews > C++ > why only var1+=var2,why not var1=+var2

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

malli
Guest
Posts: n/a

 10-21-2005
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.

David Shin
Guest
Posts: n/a

 10-21-2005
It's the syntax.

Why do we use "+" for addition and "-" for subtraction?
Why not "*" for addition and "/" for subtraction?

Pete Becker
Guest
Posts: n/a

 10-21-2005
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)

Mike Wahler
Guest
Posts: n/a

 10-21-2005

"malli" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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

Kev
Guest
Posts: n/a

 10-21-2005
"malli" <(E-Mail Removed)> wrote in news:1129918348.131683.315850

> var1=-var2

what if there was a little typo and it was

var1= -var2 ?

Pete Becker
Guest
Posts: n/a

 10-21-2005
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)

Marcus Kwok
Guest
Posts: n/a

 10-21-2005
malli <(E-Mail Removed)> 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.

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

TomHanks
Guest
Posts: n/a

 10-21-2005
> 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