# help please

Printable View

• 07-29-2003
mohammed
help please
Can someone please help me out if you can.

I am trying to understand the difference between preincrement and postincrement. I am trying to figure out the value of total when exiting each of the following while repetition structures (looking to enter the answer after each while repetition structure):

int x = 1, total = 0;
while ( ++x <= 5 ) {
total += x;
}

int x = 1, total = 0;
while ( x++ <= 5 ) {
total += x;
}

Thanks
• 07-29-2003
alpha
in the first example, the value of x is changed to 2 before it is compared.

in the second example, the value of x in the initial comparison is 1 and then will change after the comparison.

compile it, run it, and see the results.
• 07-29-2003
Hammer
Here's a simple example of pre/post increment
Code:

```x = 1; std::cout <<"Preincrementing x you get " <<++x <<std::endl; std::cout <<"x is now " <<x <<std::endl; x = 1; std::cout <<"Postincrementing x you get " <<x++ <<std::endl; std::cout <<"x is now " <<x <<std::endl;     Preincrementing x you get 2 x is now 2 Postincrementing x you get 1 x is now 2```
• 07-29-2003
Hammer