increment and decrement
(Ie. add one, or subtract one)
used all the time when looping and keeping count of things
a++ means "return the value of a, and then increment it"
++a means "increment a, then return the value"
subtle (but important) differences, however the differences between the pre & post incrementation are only important if you care about the value of a variable mid-operation
Code:
for (i = 0; i < max; i++)
{
}
and
Code:
for (i = 0; i < max; ++i)
{
}
are identical in operation
(because we do not care about the value of 'i' mid-operation
however,
say we are keeping a count of something that we later need to step back through in reverse
Code:
void increment_something(int var)
{
old_var = var++;
}
here, we do care about the value of var mid-operation, therefore we store the "old value" of var and increment var
You can of course do this
Code:
void increment_something(int var)
{
old_var = var;
var++;
}
then we've split the operation into two
and now we no longer care about the return value of the increment
Bookmarks