+ 1
i'm newbie, please help me about looping ++ --
var i=10 i++ // i=? ++i // i=? i-- // i=? --i // i=?
2 Réponses
+ 7
Some one asked something similar a while back.
the ++ / -- is increment and decrement, respectively and has 2 methods: Pre- and Post-
Pre-Increment / Pre-Decrement -
increases or decreases the value BEFORE any further code is run.
E.G x=2
y=5
z = ++x + --y // before the sum is calculated, the pre increment and pre-decrement are applied, making this effectively:
z = 3 + 4
// pre increment on x(which is 2) increases it to 3
// pre decrement on y(which is 5) decreases to 4
Post-Increment / Post-Decerement -
Increases or decreases the values AFTER processing the statement. so from above
z = x++ + y-- // Sum is calculated, 2 + 5 = 7 then X is increased by 1 and y is decreased by one, giving X and Y new values as below
x = 3
y = 4
Does that help?
For you specific example and values:
i = 10 -- > i++ // i=11 (AFTER evaluating any code)
i = 10 -- > ++i // i=11 (BEFORE evaluating any code)
i = 10 -- > i-- // i=9 (AFTER evaluating code)
i = 10 -- > --i // i=9 (BEFORE evaluating code)
+ 2
I++ first print the value and then it will allow to increment. where as ++I will first increment the value then it will print