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;
  1. Mithrandir0x (2010-12-28 05:17)

    Good evening, is there any difference by having ++ operator behind ‘a’ instead of having it in front of it?Farewell.

  2. joe (2010-12-28 05:37)

    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.

Add your comment now