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
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: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
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.
Hope is the first step on the road to disappointment.
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.
Actually, strictly speaking, I think it's more like:
Value = X++;
Whereas, Value = ++X;Code:int temp = X; X = X + 1; Value = temp;
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 optimizationsCode:X = X + 1; Value = X;
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
Why don't you just write a little program to figure this out yourself?
FAQ
"The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.
"If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.
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".