Why do people write --i?

Possible duplicate:
Increment in C ++ - When to use x ++ or ++ x?

I have 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 the case in other languages ​​of the C-family, and if so, why do some people prefer --i to i-- ?

+4
source share
6 answers

I do not believe that the prefix ( ++i ) and the postfix ( i++ ) depend on whether it is an increment or decrement.

Why do I prefer the prefix, where possible, because my native language is English, which has basically the structure of the verb in front of the object ("dine", "drive"), so "increment i " or "increment i decrement i " is more natural for me than " i decrement". Of course, I use postfix with pleasure if I use the result, and I need the value up to the increment in the expression (and not the increment).

+5
source

++i faster than i++ . What for? See a simple explanation.

i ++

i++ will increase the value of i, but will return a previously increased value.

  • temp = i
  • i increases
  • temp back

Example:

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

++ i

++i will increase the value of i, and then return the increased value.

Example:

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

This is the same for --i and i-- .

+12
source

Historically, the increment / decment operator has been motivated by stack management. When you push an item on the stack:

 *(stackptr++) = value 

when you pop:

 value = *(--stackptr) 

(they have been converted to separate ASM instructions)

So, you get used to increment-after and decrement earlier.

You can see another idiomatic example in the forward and reverse order:

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

This is pretty much the preferred thing that comes into play only if you do pre-or post-increment for types that are more complex than primitives.

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

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

+4
source

In C and C ++, the ++ and -- operators have both the prefix form: ++i and --i , and the suffix form: i++ and i-- . Former funds are evaluated and then used and then evaluated. Difference only makes sense when any form is used as part of an expression. Otherwise, it is a matter of preference or style (or lack thereof).

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

When used as an operator, that is: i++; ++i; i++; ++i; both forms are equivalent.

+2
source

The difference is a preliminary increment or post-increment (as well as a decrease).

Quoting a Wikipedia article on a topic that appears as Google’s second “decrement” result:

 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 

Thus, it is not so much a matter of preference as a difference in results.

+1
source

All Articles