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

M

malli

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

David Shin

It's the syntax.

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

Pete Becker

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

Mike Wahler

malli said:
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
 
P

Pete Becker

Pete said:
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.
 
M

Marcus Kwok

malli said:
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.
 
T

TomHanks

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top