But they should be familiar with it, or at least have seen it a few times. Looping between 0 and n-1 is a basic operation, and the most concise/efficient ways of doing it areOriginally Posted by laserlight
upwards, andCode:for (int i=0; i != n; i++) {}
downwards. These also allow using an unsigned index, and have the clarity of referring to n instead of n-1. This is a very small amount of information to store in one's bag of tricks, and doesn't require thought once one knows it. (I think the "premature optimization" quote refers to something that requires effort.) But most people, even experienced ones, won't consider doing it unless they've seen it in someone else's code. This is one of those cases where the best tool for the job has fallen through the cracks because of the self-fulfilling prophecy of everyone being afraid of confusing everyone else. The danger of temporary confusion is small compared to the possible benefit of learning an idiom which can be used routinely. This applies to maintaining other people's code as well as learning to code. I could have avoided using the idiom on the grounds that it's not needed on efficiency or range grounds here, but then jmajeremy probably never would have seen it again.Code:for (int i=n; i--;) {}