Proposal of a general do-while loop
Stewart Gordon
smjg_1998 at yahoo.com
Thu Jul 19 13:02:51 PDT 2007
"Taro Kawagishi" <tarok at acm.org> wrote in message
news:f7hplb$1ovc$1 at digitalmars.com...
<snip>
> 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.
<snip>
On seeing a while (true), I think it goes without saying that you need to
look for a break or return statement somewhere to find the terminating
condition.
BTW here are two other ways of doing your particular case:
// listing 5
size_t pos = 0;
while ((pos = text.find(pattern, 0)) != string::npos) {
cout << "pattern found at " << pos << "\n";
++pos;
}
// listing 6
for (size_t pos = 0; (pos = text.find(pattern, 0)) != string::npos;
++pos) {
cout << "pattern found at " << pos << "\n";
}
but I realise this is no use in the more general case when you've got more
to do in the portion before the break.
> 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;
> }
<snip>
I'm not sure I like this notation. There are always going to be people who
prefer to put curly brackets on a line of their own. And then, given a
snippet of code
...
}
while (cond)
{
...
it'll be necessary to scroll, possibly through several screens of code, just
to find out whether the block above the while is part of the loop as well.
Moreover, I don't know if it's a common pitfall to write
do {
...
}
(with no while clause) intending an infinite loop (or one that must be
broken out of). But should somebody fall into this trap and follow it
immediately with a while loop, it would take on a whole new meaning.
> 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.
<snip>
I'm still not sure it gains anything practically over the if-break method.
Moreover, what if you want multiple terminating conditions at different
points in the loop? You can put any number of breaks in a loop; your idea
OTOH doesn't easily extend to multiple exit points.
(For the record, Beta Basic on the ZX Spectrum had an "EXIT IF" statement,
really the equivalent of if-break though it also looked a bit like your
idea, especially as the automatic indentation would show that particular
statement at the same level as the DO and LOOP. If you like, you _could_
outdent your if-break statements to achieve the same effect....)
Stewart.
More information about the Digitalmars-d
mailing list