why just simply changing the position of ++ will give 4 feet 6 inches for both if ++inches and 6 feet 6 inches for one.inches for inches++? i don't get it anyone can explain it to me? (please ignore my syntax errors i am just being lazy).
Look at what the book said again, and try this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
int a = 0;
int b = 0;
std::cout<<"a = "<<a<<std::endl;
std::cout<<"b = "<<b<<std::endl;
a = b++;
std::cout<<"a = "<<a<<std::endl;
std::cout<<"b = "<<b<<std::endl;
a = 0;
b = 0;
std::cout<<"a = "<<a<<std::endl;
std::cout<<"b = "<<b<<std::endl;
a = ++b;
std::cout<<"a = "<<a<<std::endl;
std::cout<<"b = "<<b<<std::endl;
When you have ++b (pre-increment) you add to 1 b before assigning it to a. When you have b++ (post-increment) you assign to a first, then to add 1 to b. There isn't really a much simpler explanation than that.
jimmy... take a look at this code and output...
It won't get much simpler than this...
1 2 3 4 5 6 7 8 9 10 11 12 13
int a = 5;
int b = 7;
std::cout << "a: " << a << std::endl; // Prints 5
std::cout << "b: " << b << std::endl; // Prints 7
a = b++; // After this executes, a will be equal to 7, and b will be equal to 8.
/*
Explanation
a = 5
b = 7
a = b++ <------------ Assigns b to a, making a = 7, then increments b by 1, making b = 8.
*/
std::cout << "a: " << a << std::endl; // Prints 7
std::cout << "b: " << b << std::endl; // Prints 8
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int a = 5;
int b = 7;
std::cout << "a: " << a << std::endl; // Prints 5
std::cout << "b: " << b << std::endl; // Prints 7
a = ++b; // After this executes, a and b will both equal 8.
/*
Explanation
a = 5
b = 7
a = ++b <-------- Adds 1 to b, making b = 8, then assigns b to a, making a = 8.
*/
std::cout << "a: " << a << std::endl; // Prints 8
std::cout << "b: " << b << std::endl; // Prints 8