Friday Finking: Contorted loops

2QdxY4RzWzUUiLuE at potatochowder.com 2QdxY4RzWzUUiLuE at potatochowder.com
Sun Sep 12 13:28:22 EDT 2021


On 2021-09-11 at 18:21:17 +0100,
Alan Gauld via Python-list <python-list at python.org> wrote:

> On 11/09/2021 15:41, Peter J. Holzer wrote:
> 
> > How is C's do/while loop more horrible than Pascal's repeat/until? 
> 
> Because it is very hard to spot or distinguish from a normal
> while loop.
> 
> while condition ;
> 
> Is a valid (and fairly common) loop in C
> 
> so code that has
> 
> do{
> code
> }
> while condition;
> 
> Looks, for non-trivial cases, like a lot of code followed
> by an empty while loop.
> 
> The do is easy to miss  and the while loop disguised as
> a repeat termination is confusing.

Well, yeah, except that only a macro would ever write it that way.  :-)

At the very least, the code would be indented (making it easier to spot
the "do," before or after you see the while; and my apologies if you
intended it that way and it got lost somewhere between your intent and
my monitor):

    do {
        code;
    }
    while(condition);

(Side question:  why put the "{" next to the "do," but the "}" and the
"while" on separate lines?)

And I would put the while on the same line as the closing brace (which
is also where I put the "else" in an if statement):

    do {
        code;
    } while(condition);


More information about the Python-list mailing list