loop break

Started by
47 comments, last by Ectara 9 years, 11 months ago

for(int i=10; i--;)
{
    /* do something */
}

I quite liked it. although still getting rejected from review.

Advertisement

This is really just an obfuscated version of the idiom:


while (i--)
{
    /* do something */
}

Where i is a preexisting variable.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

I love it!

What does it do?

It does "..." 10 times, for i==9 through to i==0.
Or:
if(10) -- true, so do...with i=9
if(9) -- true, so do...with i=8
...
if(1) -- true, so do...with i=0
if(0) -- false, so end of loop
That's if I've read it correctly -- I'm assuming it was rejected from a code review due to it not being a common idiom ;-)

If you don't require backwards iteration, the standard idiom would be:
for( int i=0; i!=10; ++i )
Yeah it's more characters, but it's also more readable ;-P

As a fun tidbit, counting backwards like that can actually generate smaller and (very marginally) faster code.

Most processors use a comparison function that tests for a zero result or non-zero result and uses that to control the loop. So instead of generating an operation that says "subtract 10 from x and see if it is zero", or "compare x with ten, and then see if the results are equal", it can just say "is this zero" with no additional operation.

For instance, the x86 instruction LOOP will decrease a counter and then jump if the result isn't zero.

On a modern very fast processor it is unlikely to be of much use, saving a nanosecond or two, but back in the slow old days of counting CPU instruction timings, counting up for loops was highly discouraged.

yeah, the second parameter for a for loop is a logical condition, the use of the decrement operator will bring it to 0 (logic false) which breaks the loop.

It will fail the review, there's no way it's getting into production code, readability is everything, I did consider breaking open the assembly to see if there's a performance difference between that and a while loop but there's no need, they'll (probably) be the same .

Although it's nice to see someone who demonstrates a slightly more intuitive knowledge of the language.

What does it do?


Perhaps this slight variation, using the "goes to" operator, will clarify:

while ( i --> 0) {
    // ...
}

Loops like that make it easier to remove stuff from indexed lists as well.

I kinda like it.. as it works for unsigned integers looping down to and including zero. Could write it as i = 9 + 1 for clarity..

I like it but I can understand why it was rejected; if I was reviewing this I'd probably reject it too.

On balance it's a completely unnecessary cutesy trick: abusing the for loop syntax to save a tiny bit of typing. The kind of thing that a maintainer will discover in 6 months time, think "hold on, this doesn't look right", possibly change, and wreak havoc as a result.

Code should be expressive. If you have to stop and think before you can figure out what's going on, you've failed. Especially with something as fundamental and common as a for loop.

At the very least you should prefix it with a "warning: cutesy trick" comment, but then you've typed more than you've saved, so you're really just accomplishing nothing more than showing off your understanding of the for loop, and it's still just a cutesy trick.

I still like it though, but I'd burn it with fire. Breaks my heart.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

This topic is closed to new replies.

Advertisement