Still more than 3 ways to loop it...
  Posted April 22, 2004    PermaLink    Comments (0)  

A discussion on looping in C# misses my favorite approach to looping, of course this only works if order of execution does not matter...

for (int i = foo.Length - 1; i >= 0; i--) {}

The big advantage here is that we are not going up to some unknown number that has to be stored on the side or accessed from a (potentially mutable) field. This saves one value on the register stack

It also works if you are trying to index the first occurance, although going forward and breaking out of the loop seems to work better in those cases.

21 Apr 2004 9:15 pm MDT: Edited to explain what I think is novel about it.

Trackback URL
  TrackBack URL for this entry:
Post Comment

Thanks for signing in, . Now you can comment. (sign out)

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

Remember me?

Email Address: