### The differences between a++ and ++a

Hello everyone,I would like to ask a question here.What are the differences between a++ and ++a?

 ``123456789101112131415`` ``````do{ a++; cout<

Can someone explain briefly for these 2 examples?
In this context it doesn't do anything different.
The difference is here:

 ``12`` ``````int a = 0; int b = a++; // b = 0, a = 1 ``````

Where

 ``12`` ``````int a = 0; int b = ++a; // b = 1, a = 1 ``````
So,how do I know when a++ and ++a will do different things?
After I reading the contents of the website,I still do not know when a++ and ++a will do different things .
a++ increments a after the expression has been evaluated. So in:

 ``12`` ``````a = 1; b = a++;``````

The expression a++ evaluates to 1, so b is set to 1, and then a is incremented to 2.

++a increments a before the expression is evaluated, so in:

 ``12`` ``````a = 1; b = ++a;``````

a is incremented first, and the expression then evaluates to 2, so b is set to 2.
Last edited on
As EssGeEich said, the context of your example doesn't really do anything different. These operators are convenient for looping when you want to either used a variable's current value or its new value.

Here is an example that might help:

 ``12345678910111213`` `````` int LegalAge = 21, age = 0; while( age++ < 21 ) { cout << "Your age is " << age << "." << endl; cout << "You can't have a beer until you are " << LegalAge; << "!" << endl << endl; } ``````

In the example above, you will have the following output:

You can't have a beer until you are 21!

You can't have a beer until you are 21!

You can't have a beer until you are 21!

You can't have a beer until you are 21!

You can't have a beer until you are 21!

You can't have a beer until you are 21!

You can't have a beer until you are 21!

You can't have a beer until you are 21!

You can't have a beer until you are 21!

You can't have a beer until you are 21!

You can't have a beer until you are 21!

You can't have a beer until you are 21!

You can't have a beer until you are 21!

You can't have a beer until you are 21!

You can't have a beer until you are 21!

You can't have a beer until you are 21!

You can't have a beer until you are 21!

You can't have a beer until you are 21!

You can't have a beer until you are 21!

You can't have a beer until you are 21!

If you changed the example to:

 `` `` ``while( ++age < 21 )``

you would have the following output:

You can't have a beer until you are 21!

You can't have a beer until you are 21!

You can't have a beer until you are 21!

You can't have a beer until you are 21!

You can't have a beer until you are 21!

You can't have a beer until you are 21!

You can't have a beer until you are 21!

You can't have a beer until you are 21!

You can't have a beer until you are 21!

You can't have a beer until you are 21!

You can't have a beer until you are 21!

You can't have a beer until you are 21!

You can't have a beer until you are 21!

You can't have a beer until you are 21!

You can't have a beer until you are 21!

You can't have a beer until you are 21!

You can't have a beer until you are 21!

You can't have a beer until you are 21!

You can't have a beer until you are 21!

You can't have a beer until you are 21!

Notice that the first line starts with "Your age is 1" in this case.

I hope this helped to explain the difference.

EDIT: Thanks to cire who caught my cut and paste error!

Last edited on
Difference between both a++ and ++a are quite easy.
in a++ we are adding one in a and other side we are add a to some other variable.
 in a++ we are adding one in a and other side we are add a to some other variable.

Um... what? Unless I've totally misunderstood what you're trying to say, that's nonsense. ++a increments a by 1, just as a++ does. The difference is in what the expression evaluates to.
Last edited on
kooth wrote:
In the example above, you will have the following output:

Your output is wrong in the first case.

http://ideone.com/hvg1m3
cire you're right: It was a cut and paste error. Thanks for catching what my old eyes can't.
Topic archived. No new replies allowed.