Loop iterator - example.txt

Alexander Panek alexander.panek at brainsware.org
Sat Apr 29 02:54:09 PDT 2006


Rick C. Hodgin wrote:
> 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.
> 
> - Rick C. Hodgin
> 
> 
> 
> 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
> }

Very neat, got my vote!

Regards,
Alexander Panek



More information about the Digitalmars-d mailing list