# Thread: increment and decrement operators

1. ## increment and decrement operators

The increment and decrement operators can be placed before or after an integer variable. But I need to know the different behaviour of these two operators when they are placed before and after an integer variable, with examples

2. ## Re: increment and decrement operators

Originally posted by ee0u22ba
The increment and decrement operators can be placed before or after an integer variable. But I need to know the different behaviour of these two operators when they are placed before and after an integer variable, with examples
Try your C++ book on for size eh? Every C++ book covers this. It's exactly like it sounds:

Before increments before the value is used.
After increments afterwards.

Example:

output( c++ ); // passes thevalue of 'c' to output, then increments it

output( ++c ); // increments 'c' and then passes it to output

Not hard at all. Like I said, just look in any C++ book.

Quzah.

3. This:
Y = X++ ; // Y equals the old X value. Increment X

Is the same as these two statements:
Y = X ; // Y equals the old X value
X = X + 1 ; // Increment X

This:

Y = ++X ; // Increment X. Y = the new value

Is the same as these two statements:
X = X + 1 ; // Increment X
Y = X ; // Y = the new X value.

And:
cout << X++ ; // Prints the old value of X, then increments.
cout >> ++X ; // Increments X, then prints the new value.

4. Actually, strictly speaking, I think it's more like:

Value = X++;
Code:
```int temp = X;
X = X + 1;
Value = temp;```
Whereas, Value = ++X;
Code:
```X = X + 1;
Value = X;```
Not 100% sure, but given that operators mostly if not always act as functions do, I doubt that they're smart enough to return a value and then increment it after (If you find a way of making a function that does this, please tell me! ). That's why I generally prefer ++X... I still could be wrong though, especially with those new-fangled compiler optimizations

5. Why don't you just write a little program to figure this out yourself?

6. I like to think of the postfix version as a sort of sidenote. eg...

y = x++;

...would be "Y equals x. Oh, and increment x.", whereas

y = ++x;

...would be "Y equals x incremented".