The x++ and ++x operators

Hi, I'm newbie in programming. I hope can find answer in this forum.

First of all, I have a program/code like this:

int a, b

a = 1;
b = a++;

cout <<"a"<<a<< endl;
cout <<"b"<<b" endll

a = 1;
b = ++a;

cout <<"a"<<a<< endl;
cout <<"b"<<b" endll


a = 2, b = 1
a = 2, b = 2

Actually I was understand that the function of these operators, but sometimes I was thinking logically. The question is >> why a = 2?? I know the post-increament and post-decreament, but why a is 2?

It's clearly that when the code printed the a value, it's 1 not 2. The cout statement meant "print" the value of a, the value is 1, when the code executed why the a value become 2?

Logically in human language is:

print a = ... a value end of line
print b = ....b value with ++ operator end of line

You see? print a, a = 1. But why the output is 2??

Hmmm...I'm sorry, I don't really get it. Can the forum members can explain to me in human language?

Thank you so much if you'all will...

Best regards,

closed account (Dy7SLyTq)
a = 2 because your still incrementing it regardless of what is returns to b. the equivalent would be

int a, b;
a = 1;
b = a
a += 1;
cout<< a <<" "<< b << endl;

a = 1;
a += 1;
b = a;
cout<< a <<" "<< b << endl;
I see now...thank you so much... ^_^

Thank you for the explaination...
Topic archived. No new replies allowed.