Loop iterator - example.txt

Craig Black cblack at ara.com
Fri Apr 28 21:40:42 PDT 2006


"Rick C. Hodgin" <Rick_member at pathlink.com> wrote in message
news:e2tu2b$kv8$1 at digitaldaemon.com...
> Here's an idea:
>
> There should be a way in D to allow the reconsideration of a for..loop
test
> clause without executing the increment clause.
>
> Using the terminology:
> for (initialize-clause; conditional-clause; increment-clause)
>
> Example:
> int i;
> for (i=0; i<10; i++)
> {
> if (string.substr(i,1) == something)
> {
> i += some_other_function();
> retry;
> }
> else if (string.substr(i,1) == something_else)
> {
> i += some_other_function2();
> retry;
> }
> // Otherwise, simply execute the "i++" and re-test
> }
>
> I propose the name "retry" for the "retest without increment-clause"
command, to
> be used in a manner similar syntax-wise to the way "break" is used today.
> "Retry" would simply bypass the increment-clause and proceed straight to
the
> conditional-clause code section, thereby allowing subsequent passes
through the
> for loop without the requisite and occasionally unnecessary
auto-incrementation.
>
> It would just be a way to give for loops a little more natural utility
without
> having to do some rather obtuse programming techniques, such as using
goto's or
> enclosing the code in a while or do loop, etc.

Very good idea.  I find myself decrementing the iterator in order to achieve
the same. This approach doesn't work in more complex cases and I've recently
run into a complex situation where "retry" would have saved me a lot of
headaches.

-Craig





More information about the Digitalmars-d mailing list