No Do while/repeat until looping construct in python?

Alex Martelli aleax at aleax.it
Wed Mar 12 11:20:02 EST 2003


Lars W wrote:

> Why is there no do-while looping construct in Python?

Because it's basically redundant.

> It seems to me that such a feature is very useful. Consider the
> following pseduocode:
> 
> <code>
> while i<j:
> <code>

Coding this way would be terrible.  Never duplicate code.

> <code> might be a large chunk of code which has to be run at
> least once before the evaluation of "i<j" is made. But Python
> forces you to write code on the first form.

There is absolutely no such "forcing".  Just code it:

while True:
    <code>
    if i >= j: break

and there you are.


> I might be missing something, but do-while is indeed a
> useful looping construct which users would benefit from using.

If you're looking for large, complicated languages, which take
pride in offering many ways to perform each task, then Python
is not the right place to look, but you have a huge variety of
other choices.  In Python, it's considered preferable to have
one obvious way to perform a task, to keep the language simpler.

The general case of loop, known as "N times and a half", is:

while True:
    <precode>
    if <sometest>: break
    <postcode>

You appear to think (like the designers of some other languages)
that the special case of <postcode> being empty has such enormous
importance as to warrant making the language richer and more
complicated to cover it.

The designer of Python disagrees with you.

Which is why I'm using Python -- I entirely agree with this design
decision and philosophy.

If you don't, I earnestly beseech you to pick any of a dozen other
languages, designed with different philosophies, rather than trying
to make Python richer with redundant constructs.


Alex





More information about the Python-list mailing list