4

Possible Duplicate:
Incrementing in C++ - When to use x++ or ++x?

I've seen things like i++ commonly used in, say, for loops. But when people use -- instead of ++, for some reason some tend to write --i as opposed to i--.

Last time I checked, they both work (at least in JavaScript). Can someone tell me if this is so in other C-family languages, and if it is, why do some people prefer --i to i--?

Community
  • 1
  • 1
Bluefire
  • 13,519
  • 24
  • 74
  • 118

6 Answers6

12

++i is faster than i++. Why? See the simple explication.

i++

i++ will increment the value of i, but return the pre-incremented value.

  • temp = i
  • i is incremented
  • temp is returned

Example:

i = 1;
j = i++;
(i is 2, j is 1)

++i

++i will increment the value of i, and then return the incremented value.

Example:

i = 1;
j = ++i;
(i is 2, j is 2)

This is simillar for --i and i--.

Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
  • 1
    But why would anyone do `i++` but `--i`? That's the question. – Bo Persson Jan 27 '13 at 10:37
  • The compiler can easily detect if the temp is used or not. So I would doubt that the code is faster. – Emanuele Paolini Jul 03 '13 at 15:01
  • 1
    @EmanuelePaolini: Yeah. There's an entire [question devoted to that for C](http://stackoverflow.com/questions/24886/is-there-a-performance-difference-between-i-and-i-in-c). TL;DR - Any vaguely-recent compiler will optimize away the difference, probably even if you *do* use the resulting value. In Java, the bytecode is identical if you don't use the result. If you do, it's just a matter of the order of the opcodes (with local ints, I didn't go in depth), `iinc / iload_1` vs. `iload_1 / iinc`. Of couse, the JIT may get involved at some point. Still, since I prefer prefix... ;-) – T.J. Crowder Nov 10 '15 at 18:34
5

I don't believe the preference for prefix (++i) vs. postfix (i++) varies depending on whether it's an increment or a decrement.

As for why, I prefer prefix where possible because my native language is English, which has mostly a verb-before-object structure ("eat dinner", "drive car"), so "increment i" or "decrement i" is more natural for me than "i decrement". Of course, I'll happily use postfix if I'm using the result and I need the value prior to the increment in an expression (rather than the incremented value).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • uh prefix is "always" at least as efficient as postfix, and might be more efficients. As explained http://stackoverflow.com/a/1813008/511302 here – paul23 Jan 27 '13 at 10:37
5

Historically the increment/decrement operator were motivated by stack management. When you push an element on the stack:

*(stackptr++) = value

when you pop:

value = *(--stackptr)

(these were converted to single ASM instructions)

So one gets used to increment-after and decrement-before.

You can see another idiomatic example in filling in direct and reverse order:

for (p=begin;p!=end;) 
    *(p++) = 42;

for (p=end;p!=begin;) 
    *(--p) = 42;
Emanuele Paolini
  • 9,912
  • 3
  • 38
  • 64
4

It's pretty much a preference thing, that only comes into play if you're doing the pre- or post-increment on types that are more complicated than primitives.

Pre-increment ("++i") returns the value of i after it has been incremented, and post-increment ("i++") returns the value before increment. So, if i was of a complicated type that had overloaded the pre-increment and post-increment operators, the pre-increment operator can just return the object, whereas the post-increment would have to return a copy of the object, which could well be less efficient if the object is substantial. And in most cases (for loop increments, etc.), the value is ignored anyways.

Like I said, most of the time, not a problem, and just a preference thing.

sheu
  • 5,653
  • 17
  • 32
2

In C and C++,++ and -- operators have both prefix form: ++i and --i, and suffix form: i++ and i--. Former means evaluate-then-use and latter means use-then-evaluate. The difference is only relevant when either form is used as part of an expression. Otherwise it's a matter of preference or style (or lack thereof).

F.ex., in int i = 0; int n = ++i;, i is first incremented and then its value, now 1, is assigned to n. In n = i++ value of i, still 1, is assigned to n and is then incremented, with i == 2 now.

When used as a statement, that is: i++; ++i; both forms are equivalent.

Martin Green
  • 1,056
  • 8
  • 15
  • The last statement in your post is not entirely true. For basic types, the compiler may optimize by replacing `i++` with `++i` if the result isn't used. If it doesn't perform this optimization, you may get a (very small) difference in performance. However, for user-defined types with overloaded `operator++`, this optimization is not allowed (because the compiler can't guarantee that you've correctly implemented the two to have the expected relationship). This means that, for user-defined types in C++, which form you choose actually affects the result. – Agentlien Jan 27 '13 at 10:48
1

The difference is pre-incrementing or post-incrementing (and likewise with decrementing).

Quoting the Wikipedia article on the topic that shows up as the second Google result for "c decrement":

int  x;
int  y;

// Increment operators
x = 1;
y = ++x;    // x is now 2, y is also 2
y = x++;    // x is now 3, y is 2

// Decrement operators
x = 3;
y = x--;    // x is now 2, y is 3
y = --x;    // x is now 1, y is also 1

So, it's not so much a matter of preference as a matter of difference of results.