Hack of the day: Automatic reset conditions
December 28, 2010
You often need conditions that reset variables during programming. For example:
if (a >= 800)
a -= 800;
++a;
You can achieve the exact same behaviour with one single line of code using the modulo operator:
a = (++a) % 800;
Good evening, is there any difference by having ++ operator behind ‘a’ instead of having it in front of it?Farewell.
yeah, in some cases it’s a little bit faster to use the ++ operator before. the difference is, that if you use it before the variable, it increments the value and returns a reference to itself.if you use the ++ operator after the variable, it increments the value and returns the value of the object BEFORE it was incremented. so one additional copy operation is required.i think i will write a blog post about it, soon.