Loop iterator - example.txt

Bruno Medeiros brunodomedeirosATgmail at SPAM.com
Sat Apr 29 13:02:10 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
> }

For the case where you don't have any continues, you can do the code 
this way instead:

   for (i=0; i<10; )
   {
     if (string.substr(i,1) == something)
     {
       i += some_other_function();
       continue;
     }
     else if (string.substr(i,1) == something_else)
     {
       i += some_other_function2();
       continue;
     }
     // ############ Execute the increment expression here: #######
     i++;
   }

The remaining case, where you want to use continues and retries in the 
same for, well, I don't think it's a common enough case that makes it 
worth the introduction of a new keyword just some trivial syntactic sugar.
In fact, the very idea seems like a very awkward idiom to me. I would 
like to examine a real example, can someone post one?



-- 
Bruno Medeiros - CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D



More information about the Digitalmars-d mailing list