Proposal of a general do-while loop

downs default_357-line at yahoo.de
Tue Jul 17 09:25:01 PDT 2007


Taro Kawagishi wrote:
> I think a more natural way to express the logic is to write the code as in listing 4.
> 
>     // listing 4
>     size_t pos = 0;
>     do {
>         pos = text.find(pattern, pos);
>     } while (pos != string::npos) {
>         cout << "pattern found at " << pos << "\n";
>         ++pos;
>     }
> 
> The meaning of
> 
>     do {
>         aa;
>     } while (bb) {
>         cc;
>     }
> 
> is
> 
>     while (true) {
>         aa;
>         if (not bb) {
>             break;
>         }
>         cc;
>     }

void doWhile(void delegate() pre, lazy bool cond, void delegate() post) {
   while (true) {
     pre;
     if (!cond()) break;
     post;
   }
}

// listing 4, modified
size_t pos=0;
doWhile ({
   pos=text.find(pattern, pos);
}, pos!=string::npos, {
   writefln("Pattern found at ", pos);
   ++pos;
});

Not tested, but should work.
Have fun!



More information about the Digitalmars-d mailing list