From: Saverio Miroddi Date: 2011-01-15T18:52:22+09:00 Subject: Re: Normal For Loop > I tried to do this the other night, but right now I can't find a good > example so here it goes: > > a = some_array > minValue = 999999 > for(i=0; i minValue = (5000 - a[i]).abs > } > > > This isn't the best example. However, there are many times like the > loop above where I want to go through the whole thing, but if I find > exactly what I am looking for, I want to bail out early instead of > wasting that processing time. > > Also, I sometimes may want to not actually iterate straight through, but > browse through the array in some more complex order. I have a "good" example. In my case I want to iterate a collection, skipping a variable number of elements based on a given element. In this case, the C for construct (counter_init, cycle_condition,) would be useful because I wouldn't have to initialize the counter variable outside of the scope, and I can increment the counter inside the loop. The code is something like this: for ( i = 0; i < elements.size; ) do if elements[ i ].is_a?( OneType ) do_operation( elements[ i ] ); i += 1; elsif elements[ i ].is_a?( AnotherType ) sub_elements = [] begin sub_elements << elements[ i i += 1; end while elements[ i ].is_a?( AnotherType ) do_another_operation( sub_elements ) else ... end end This example doesn't pretend to be clever/correct/well constructed, but is a real-world case where this kind of loop would be useful. -- Posted via http://www.ruby-forum.com/.