Please can someone clearly explain to me in a very detailed layman understanding manner. The actually difference between --i and i-- And where exactly should which be used over which. Example or pictorial explanations is welcomed. I have been into programming and I understand other complex stuffs like recursion, iteration, control structure etc but I haven't really got a full understanding of that increment operator. I just use any for any of my codes in my projects. Please help.
-
3Python has neither of those... – UnholySheep Jul 20 '22 at 15:41
-
4They're decrements not increments. – evolutionxbox Jul 20 '22 at 15:43
-
4And there's plenty of results when you search for "difference between pre and post increment", such as https://stackoverflow.com/questions/484462/difference-between-pre-increment-and-post-increment-in-a-loop – UnholySheep Jul 20 '22 at 15:43
-
Does this answer your question? [Difference between pre-increment and post-increment in a loop?](https://stackoverflow.com/questions/484462/difference-between-pre-increment-and-post-increment-in-a-loop) – Mime Jul 20 '22 at 15:43
-
Does this answer your question? [What is the difference between prefix and postfix operators?](https://stackoverflow.com/questions/7031326/what-is-the-difference-between-prefix-and-postfix-operators) – ericksonb Jul 20 '22 at 15:44
-
@shadygray They behave the same way as decrements ++i and i++.:) – Vlad from Moscow Jul 20 '22 at 15:50
-
1Which language? Their behavior isn't exactly the same across C and JavaScript. – John Bode Jul 20 '22 at 15:50
-
@shadygray By the way in C you can find also multiplications like **i.:) – Vlad from Moscow Jul 20 '22 at 15:52
-
Don't spam language tags. Only tag the language relevant to the question. Since the answers given are JS, I've removed the C and Python. Feel free to adjust if incorrect. – ikegami Jul 20 '22 at 15:56
3 Answers
let count;
count = 0;
console.log('count--',count--);
count = 0;
console.log('--count',--count);
// after either count-- or --count the value of count will be -1
// but the difference is the immediate value returned by the operation

- 1,561
- 16
- 20
To clarify you can read
- the "--" or "++" as 'decrement'/'increment'
- the variable name as 'use the value'
So with --i
you decrement then use the value
But with i--
you use the value then decrement
In real use cases it mean
function print(value) {
console.log(value)
}
let i = 10;
print(i--) //Use then decrease => use i as a 10 then after using it in the function i is decreased
//The function is called like print(10)
//From here, the value of i is 9
let j = 10;
print(--j) //Decrease then use => decrese j to 9 then call the function with the decreased value
//The function is called like print(9)
//From here, the value of j is 9
j = ++i //j is set to 10 and i is 10 too
i = j++ //i is still ten but j become 11
(And just to be clear, python do not have those operators, only the +=
and -=
)

- 1,456
- 8
- 20
Ikegami edited the language tags while I was writing this, so it may not be relevant anymore
This is easier if you pick a single language.
With respect to C:
- The result of the expression
i--
is the current value ofi
; as a side effect, the value stored ini
is decremented. Given the code:
the value stored inint i = 2; int x = i--;
x
will be 2 and the value stored ini
will be 1. It's logically equivalent to writing:
with the caveat that the updates totmp <- i x <- tmp i <- i - 1
x
andi
can occur in any order, even simultaneously. - The result of the expression
--i
is the current value ofi
minus 1; as a side effect, the value stored ini
is decremented. Given the code:
the value stored inint i = 2; int x = --i;
x
will be 1 and the value stored ini
will be 1. It's logically equivalent to writing:
with the same caveat as above.tmp <- i - 1 x <- tmp i <- i - 1
Note that the side effect does not have to be applied immediately upon evaluation; it only has to be applied before the next sequence point. If you have an expression like z = x-- * --y;
, the updates to x
and y
may not be applied until after z
has been assigned:
tmp1 <- x
tmp2 <- y - 1
z <- tmp1 * tmp2
x <- x - 1
y <- y - 1
or the updates may be applied immediately:
y <- y - 1
z <- x * y
x <- x - 1
or some other order. For this reason among several others, expressions like i-- * i--
are undefined - they're not guaranteed to yield a consistent result.
I can't speak to JavaScript - I know the results are the same (i--
yields the current value of i
, --i
yields the current value of i
minus 1) and they have the same side effects, but I don't know how the side effects are handled, whether they're applied immediately or not, whether expressions like i-- * i--
are meaningful, etc.
AFAIK Python doesn't have autoincrement/decrement operators.

- 119,563
- 19
- 122
- 198