The differences between a++ and ++a

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
do{
      a++;
      cout<<a;

}while (a < 5);


//this is the other code

do{
      ++a;
      cout<<a;

}while (a < 5);


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

1
2
int a = 0;
int b = a++; // b = 0, a = 1 


Where

1
2
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:

1
2
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:

1
2
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:

1
2
3
4
5
6
7
8
9
10
11
12
13

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:

Your age is 1.
You can't have a beer until you are 21!

Your age is 2.
You can't have a beer until you are 21!

Your age is 3.
You can't have a beer until you are 21!

Your age is 4.
You can't have a beer until you are 21!

Your age is 5.
You can't have a beer until you are 21!

Your age is 6.
You can't have a beer until you are 21!

Your age is 7.
You can't have a beer until you are 21!

Your age is 8.
You can't have a beer until you are 21!

Your age is 8.
You can't have a beer until you are 21!

Your age is 10.
You can't have a beer until you are 21!

Your age is 11.
You can't have a beer until you are 21!

Your age is 12.
You can't have a beer until you are 21!

Your age is 13.
You can't have a beer until you are 21!

Your age is 14.
You can't have a beer until you are 21!

Your age is 15.
You can't have a beer until you are 21!

Your age is 16.
You can't have a beer until you are 21!

Your age is 17.
You can't have a beer until you are 21!

Your age is 18.
You can't have a beer until you are 21!

Your age is 19.
You can't have a beer until you are 21!

Your age is 20.
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:

Your age is 1.
You can't have a beer until you are 21!

Your age is 2.
You can't have a beer until you are 21!

Your age is 3.
You can't have a beer until you are 21!

Your age is 4.
You can't have a beer until you are 21!

Your age is 5.
You can't have a beer until you are 21!

Your age is 6.
You can't have a beer until you are 21!

Your age is 7.
You can't have a beer until you are 21!

Your age is 8.
You can't have a beer until you are 21!

Your age is 8.
You can't have a beer until you are 21!

Your age is 10.
You can't have a beer until you are 21!

Your age is 11.
You can't have a beer until you are 21!

Your age is 12.
You can't have a beer until you are 21!

Your age is 13.
You can't have a beer until you are 21!

Your age is 14.
You can't have a beer until you are 21!

Your age is 15.
You can't have a beer until you are 21!

Your age is 16.
You can't have a beer until you are 21!

Your age is 17.
You can't have a beer until you are 21!

Your age is 18.
You can't have a beer until you are 21!

Your age is 19.
You can't have a beer until you are 21!

Your age is 20.
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.