Jump to page: 1 24  
Page
Thread overview
Proposal of a general do-while loop
Jul 17, 2007
Taro Kawagishi
Jul 17, 2007
Gregor Richards
Jul 21, 2007
janderson
Jul 17, 2007
Regan Heath
Jul 17, 2007
Alex Burton
Jul 17, 2007
Don Clugston
Jul 17, 2007
Jeff Nowakowski
Jul 17, 2007
BCS
Jul 17, 2007
Tristam MacDonald
Jul 17, 2007
BCS
Jul 17, 2007
Tristam MacDonald
Jul 17, 2007
downs
Jul 17, 2007
downs
Re: Proposal of a general do-while loop [more generic and tested version]
Jul 17, 2007
downs
Re: Proposal of a general do-while loop [more generic and tested
Jul 18, 2007
Taro Kawagishi
Jul 18, 2007
Bruno Medeiros
Re: Proposal of a general do-while loop [more generic and tested
Jul 18, 2007
Steve Teale
Jul 18, 2007
Bill Baxter
Jul 17, 2007
Benjamin Phillips
Jul 18, 2007
Christopher Wright
Jul 21, 2007
Charles D Hixson
Jul 21, 2007
janderson
Jul 17, 2007
Manfred Nowak
Jul 17, 2007
Frank Benoit
Jul 17, 2007
Henning Hasemann
Jul 17, 2007
Ary Manzana
Jul 17, 2007
Joe Gottman
Jul 18, 2007
Taro Kawagishi
Jul 18, 2007
Roberto Mariottini
Jul 18, 2007
Regan Heath
Jul 18, 2007
Bruno Medeiros
Jul 18, 2007
Manfred Nowak
Jul 19, 2007
Stewart Gordon
Jul 21, 2007
Charles D Hixson
Jul 22, 2007
Rioshin an'Harthen
July 17, 2007
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.

July 17, 2007
while ((pos = text.find(pattern, pos)) != string::npos) {
    ...
}


Yeesh.

 - Gregor Richards
July 17, 2007
for (size_t pos = text.find(pattern, 0); pos != string::npos; pos = text.find(pattern, pos)) {
    cout << "pattern found at " << pos << "\n";
    ++pos;
}
July 17, 2007
I agree with Taro.

I also have felt this uneasiness with this type of loop.

I think it is an excellent proposal.
July 17, 2007
On Tue, 17 Jul 2007 03:04:11 -0400, 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.

I dont see any reason to further complicate the language for this. As you already pointed out yourself it can be easily accomplished with the current language features.

Here's mine:

for(;;)
{
	aa;
	if (!cond) break;
	bb;
}

- Tomas
July 17, 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.
> 

Forth has this construct in the form of a BEGIN ... WHILE ... REPEAT loop. I don't think I've seen it elsewhere, though. Uses for it come up fairly frequently in my experience, but as others have mentioned, the for(;;) { aa; if (cond) break; bb; } idiom isn't too bad.
Walter uses 'goto' more than any other programmer I've ever seen. Search through Phobos and the DMD front-end for 'goto', and see how many could be replaced by your do-while loop. Is it a significant fraction of the total?
July 17, 2007
Don Clugston wrote:
> Walter uses 'goto' more than any other programmer I've ever seen.

Now there's a good D usenet sig :)

-Jeff
July 17, 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!
July 17, 2007
downs wrote:
> void doWhile(void delegate() pre, lazy bool cond, void delegate() post) {
>   while (true) {
>     pre;
Er, naturally, that has to be pre();
>     if (!cond()) break;
>     post;
And post();
>   }
> }
July 17, 2007
Reply to Don,

> Walter uses 'goto' more than any other programmer I've ever seen.

I'd do this:

goto mid;
while(bb)
{
 cc;
mid:
 aa;
}


« First   ‹ Prev
1 2 3 4