Proposal of a general do-while loop

Bruno Medeiros brunodomedeiros+spam at com.gmail
Wed Jul 18 07:13:26 PDT 2007


Taro Kawagishi wrote:
> Hello all,
> 
> every once in a while I feel uneasy when I find I can't fit my logic into a do-while or while loop in a concise way.
> Here is a C++ example:
> 
> void
> find_string_occurrences(const string& text, const string& pattern) {
> 
>     // listing 1
>     size_t pos = text.find(pattern, 0);
>     while (pos != string::npos) {
>         cout << "pattern found at " << pos << "\n";
>         ++pos;
>         pos = text.find(pattern, pos);
>     }
> 
> }
> 
> The way the code is written might look redundant in calling find() twice, but I think it is reasonable because you can test the loop condition only after you run function find() but here you can't use a do-while loop which doesn't allow you to place other statements after the condition statement.
> 
> I can write the same logic as in listing 2 and 3 below, but their meanings would be less clear than listing 1, because the looping condition is in the if statement together with the break statement in it, and you need to spot the if statement in the while body to understand it.
> 
>     // listing 2
>     size_t pos = 0;
>     while (true) {
>         pos = text.find(pattern, pos);
>         if (pos == string::npos) {
>             break;
>         }
>         cout << "pattern found at " << pos << "\n";
>         ++pos;
>     }
> 
>     // listing 3
>     size_t pos = 0;
>     do {
>         pos = text.find(pattern, pos);
>         if (pos == string::npos) {
>             break;
>         }
>         cout << "pattern found at " << pos << "\n";
>         ++pos;
>     } while (true);
> 
> 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;
>     }
> 
> and is a natural extension to both of
> 
>     do {
>         aa;
>     } while (bb);
> 
> and
> 
>     while (bb) {
>         cc;
>     }
> 
> The current while loop and do-while loop will be specialized forms of this general do-while loop.
> 
> The advantage of the new construct will be seen if you have more complex statements within do and while blocks.
> I believe allowing this extended construct will be smooth since it will not break the existing code.
> I think D language would be a great fit to have this feature because the language seems to be still evolving.
> 


Argh no! Another foreach_reverse crappy redundant construct. There are 
at least 3 alternatives in which *IMO* any of them is better than adding 
a language construct, so votes -= 3 !

Tomas Olsen's:
   while(true) {
     aa...
     if (!cond...) break;
     bb...
   }

Gregor's:
   size_t pos = 0;
   while ((pos = text.find(pattern, pos)) != string::npos) {
     ...
   }

down's:
   doWhile(
     pos = text.find("i", pos),
     pos + 1,
     { writefln("Hit at ", pos); ++pos; }
   );


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



More information about the Digitalmars-d mailing list