473,848 Members | 1,728 Online

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

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.

Oct 21 '05 #1
7 2488
It's the syntax.

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

Oct 21 '05 #2
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)
Oct 21 '05 #3

"malli" <ma*********@gm ail.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.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
Oct 21 '05 #4
"malli" <ma*********@gm ail.com> wrote in news:1129918348 .131683.315850
@g14g2000cwa.go oglegroups.com:
var1=-var2

what if there was a little typo and it was

var1= -var2 ?
Oct 21 '05 #5
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)
Oct 21 '05 #6
malli <ma*********@gm ail.com> 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
Oct 21 '05 #7
> 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

Oct 21 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.