Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

In college it is taught how you can do math problems which use the ++ or -- operators on some variable referenced in the equation such that the result of the equation would yield different results if you switched the operator from postfix to prefix or vice versa. Are there any real world applications of using postfix or prefix operator where it makes a difference as to which you use? It doesn't seem to me (maybe I just don't have enough experience yet in programming) that there really is much use to having the different operators if it only applies in math equations.

EDIT: Suggestions so far include:

  1. function calls //f(++x) != f(x++)
  2. loop comparison //while (++i < MAX) != while (i++ < MAX)
  3. operations on objects where ++ and -- have been overloaded
share|improve this question
    
Basically every time you use it as part of a larger expression... –  delnan Mar 19 '11 at 16:41
    
    
@Martin... its funny you should mention that discussion... its actually what caused this question to re-surface in my mind! lol –  Kenneth Mar 19 '11 at 18:48
add comment

8 Answers

up vote 7 down vote accepted

prefix and postfix are not necessary, they are just shortcuts.

y = x++;

means

y = x;
x = x + 1;

however y = ++x; means

x = x + 1;
y = x;

Likewise, calling the function f as f(x++) is not the same as saying f(++x)

share|improve this answer
    
Oh... I hadn't thought about using it in a function call... –  Kenneth Mar 19 '11 at 18:14
    
To my understanding the ++ and -- operators mapped directly to a PDP-11 machine instruction. –  user1249 Mar 19 '11 at 23:14
add comment

In the C language, postfix vs. prefix apply their increments at different times and so have crucially different effects on the logic of code:

#define MAX 5
int i = 0;

This line: while ( ++i < MAX ) { ... } // sets i to 1, then compares 1 to MAX
is vastly different from while ( i++ < MAX ) { ... } // compares 0 to MAX, then sets i to 1

Yes, these operators might be shortcuts sometimes, but here the difference is very handy.

-- pete

share|improve this answer
add comment

If the operation is on a class where a copy is an expensive operation - and hasn't been properly implemented as a reference - then it might matter.

For POD - code that which makes the code most readable

share|improve this answer
    
I'm not sure if I'm following what you're saying... –  Kenneth Mar 19 '11 at 18:46
    
i++ involves an extra copy of the incremented value back to the caller, for an int this is irrelevent, for a "++CustomerClass" where a copy involves disk writes or database locks this could be bad - espcially if the class had been written badly so these couldn't be optimised away –  Martin Beckett Mar 19 '11 at 18:48
    
can the ++ and -- operators be applied to classes? I guess if you overloaded the operator it could be but naturally no correct? –  Kenneth Mar 19 '11 at 18:50
    
Correct you have to implement it for you own types –  Martin Beckett Mar 19 '11 at 20:04
add comment

For the benefit of people other than Kenneth, the point of the prefix operator is to "increment this", and the point of the postfix operator is to "increment this, but if I'm used in a longer expression, use the old value, not the new value".

Thus, any time they're used in a larger expression they will typically yield different results to each other. For instance, compare:

int countdown = 10;
while (countdown != 0) {
  cout << (countdown++) << "...\n";
}
cout << "BOOM!";

with:

int countdown = 11;
while (countdown >=1 ) {
  cout << (--countdown) << "...\n";
}
cout << "BOOM!";

or

int countdown = 10;
do {
  cout << (countdown) << "...\n";
  --countdown;
} while (countdown);
cout << "BOOM!";

Notice the subtle differences. To me, this example reads easier when (1) the initialised number if the first number printed (2) the decrement is part of the larger expression. (In real life, you'd use a "for" loop for this particular example, but I think there will always be some examples where prefix or postfix is slightly clearer.)

However

However, in modern code writing, 99% of the time I find it easier to read in the long run if the increment is always a separate statement, even if it's slightly longer. So the difference never actually really matters, if there were only one increment operator that couldn't be used in an expression itself, it would rarely impact me.

Summary

  1. If you use postfix or prefix increment operators in an expression, you should use the one that does what you mean, not the other one. If you don't you will almost always get the wrong answer[1].

  2. However, usually DON'T use them in an expression, in which case it doesn't matter much which you use.

  3. But if it doesn't matter, use prefix because that declares your intention of "increment without caring about the return value" better and may occasionally matter performance-wise (see the question linked in the comments under the article).

[1] Also be aware of the usual caveats like never increment the same value twice in one statement. This doesn't work or doesn't do what you expect on most compilers, and would be unclear even if it did.

share|improve this answer
    
Really I was trying to steer the conversation away from what the difference between prefix and postfix operators are. I understand other people will be looking at this but that wasn't really the point of the question and I thought it had already been well defined by other posts as to what their differences were. I apologize for the misunderstanding of that edit. –  Kenneth Mar 20 '11 at 0:01
add comment

Code Golf

When you need to iterate in special cases with very small amounts of code, prefix and post-fix operators can make some significant character differences.

In any other situation, they are syntactic Aspartame as they can lead to confusion when you're not paying very careful attention to what order the operations are happening in.

share|improve this answer
add comment

In C++ if you have an object that has a ++ overload and you are going through a loop generating x number of said objects, postfix can be rather expensive.

I've done this on special numeric types I've made.

vector<Fraction> values;
Fraction base;
for(int i=0; i<someVar; i++)  //shouldn't do that "i++" cuz it is inconsitent..but i do
{   
    values.push_back(Fraction);
    ++Fraction;
}

This is an example, I don't think I've used it in such a loop persay but I have needed it in loops with Fraction type before.

share|improve this answer
add comment

I code in an otherwise dead form of assembly mostly so I may have forgotten this already, but I believe this is useful in looping if you want to use the counter inside the loop without it being incremented at the beginning of the loop.

Somewhat on topic, this question has some simple usage examples in java: http://stackoverflow.com/questions/1094872/is-there-a-difference-between-x-and-x-in-java

share|improve this answer
add comment

No, there are no such real-world applications.

Pre- and postfix operators are just a convenience (syntactic sugar if you like). There are plenty of languages that don't have these operators at all (Python, for one). In any case it is always straightforward to tweak the code from pre-to-post or vice-versa and have no impact on the result.

share|improve this answer
    
Not so: there are plenty of such 'real-world applications' and plenty of opportunities to make your code more readable and maintainable using these operators. –  Pete Wilson Mar 27 '11 at 14:43
    
@Pete — I'm not convinced. I pointed out that there are plenty of languages that don't have pre- and postfix operators. I understand the importance of understanding which of pre- and post- you're using, but I still think it's straightforward for the average programmer to switch between the two. As for readability and maintainability, if Python lacking pre and post doesn't convince you we're likely to get mired in differences of opinion. –  kojiro Mar 28 '11 at 0:41
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.