473,848 Members | 1,728 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.
Advance thanks .

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
Kev
"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.
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
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.

Similar topics

20
37409
by: Brad Eck | last post by:
"The only operators that cannot be overloaded are :: (scope resolution), . (member selection), and .* (member selection through pointer to function). Quoting from Stroustrup's 3rd edition of _The C++ Programming Language_, section 11.2 (page 263), these three operators 'take a name, rather than a value, as their second operand and provide the primary means of referring to members. Allowing them to be overloaded would lead to subtleties.'"...
1
2450
by: Christopher M. Lusardi | last post by:
Hello, Is there anyway to do it other than using : #ifdef VAR1 ... #endif
6
4705
by: deanfamily11 | last post by:
I've set up a case statement to have my program determine where on the Cartesian plane a point the user enters is located. I keep getting the C2051 error when I compile. Any help? #include <iomanip> #include <iostream> using namespace std;
1
2425
by: Lucas Tam | last post by:
Hello, Does anyone know of a VB.NET class that can parse a string based on an input mask? I would like to parse a string like: %Filename% - %Date%-%Number% Of course the format of the string could change like: FillerText%Filename% - %Date%-%Number%.zip
45
3974
nemisis
by: nemisis | last post by:
Hi Everyone I have am doing an object oriented C++ program and I have no idea as to how start it................. I am not that good at coding so i tried and made an overview of what the main driver should look like main{
6
4979
by: Andrew Tatum | last post by:
I'm having some problems with the below equation. I have no problems when it comes to positives. Negatives create the problem.. C 2 1 4 However, this doesn't work:
5
2369
by: coreyover4 | last post by:
I am new just now taking classes on excel and using VB and VBA. Anyways I am having difficulties doing a code. Right now I have: Option Explicit Public Sub Problem4() Dim Var1 As Long, Var2 As Long, Var3 As Long 'Input 3 numbers from user Var1 = InputBox("Enter the first integer :", "First") Var2 = InputBox("Enter the second integer :", "Second") Var3 = InputBox("Enter the third integer :", "Third") ...
8
1290
by: callumagus | last post by:
Python Version 2.0 Windows Vista (I know, I know...) Im very new to python and have only been doing it for two weeks. print 'Type in lowercase!'; String_Var = raw_input ('Add, Divide, Multiply or Subtract? '); if 'add' in String_Var:
8
11354
by: BahatiSiD | last post by:
int y; void square(int x, int& result) { result = x*x; } ... square(3,y); this code compiles and works in DevCpp (native C++), but not in VS2008 .net C++
0
9738
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11001
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10664
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9500
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7894
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7069
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4544
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4137
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3175
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.