logoalt Hacker News

AlotOfReadingyesterday at 10:58 PM2 repliesview on HN

I really don't see what's supposedly awful about that loop, but if you want to count down to x instead of 0 you just do:

    for (size_t i = size - 1; i >= x; i--)

Replies

dataflowyesterday at 11:00 PM

> I really don't see what's supposedly awful about that loop

The stopping condition is incredibly confusing and non-obvious. Misleading at first glance, in fact. The whole thing is so unidiomatic that I don't think I've even seen it once in my life. It's a better contender for an underhanded C++ code contest than production code.

> but if you want to count down to x instead of 0 you just do i >= x

No you can't. That fails if x == 0. Which perfectly illustrates why using unsigned everywhere isn't so great. And I say this as someone who likes unsigned types and uses them more than average!

show 1 reply
StellarScienceyesterday at 11:07 PM

> I really don't see what's supposedly awful about that loop

Exactly! That's precisely the problem with it.

(Hint: think about your code when size = 0.)

show 1 reply