Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do people write --i? [duplicate]

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--?

like image 340
Bluefire Avatar asked Nov 28 '22 11:11

Bluefire


1 Answers

++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--.

like image 118
Ionică Bizău Avatar answered Dec 05 '22 06:12

Ionică Bizău