With the following for-loop you can decrement x until it reaches zero:

for(int x = 10; x --> 0;) {
        std::cout << x << std::endl; // prints out 9 to 0
    }

It looks like c/c++ has a go down operator (–>). But that’s not true: Instead two operations are executed (decrementation of x followed by a bigger than operation). It’s getting more clear what really happens if you format the code like this:

for(int x = 10; (x--) > 0;) {
        std::cout << x << std::endl; // prints out 9 to 0
    }
  1. Seanba (2010-12-29 04:01)

    I had to do a double-take at the first code snippet there before reading the rest of the blog entry to see what’s really going on.Kind of an intesting trick, and good to know. But to be honest, I wouldn’t allow anyone to submit code like that on one of our games. Do you guys use it?

  2. joe (2010-12-29 05:19)

    No! I haven’t used it in a real project yet, I also prefer the “traditional” for-loops.

Add your comment now