Both of them will add 1 to a numeric value but return different results.
i++ returns the original value (before increment).
++i returns the incremented value.
Here's an example:
Output:
Line 10
shows the main difference between post and pre increment - the returned result
The same logic applies to i--
and --i
.
For loop with no body will still repeat many times as long as it's condition is met.
This means that variable's modifications such as i++
, i--
are still performed.
Let's look at the example:
Output:
In this example the loop repeats 5 times until i
reaches value of 5.