[Tutor] repeat

Alan Gauld alan.gauld at btinternet.com
Sat Nov 17 09:41:38 CET 2007


"Michael" <python at mrfab.info> wrote

> This has probably been asked before but can I get some clarification 
> on
> why Python does not have a repeat...until statement,

Because Guido didn't put one in

repeat/until is never needed it is only ever a nice to have.
Indeed some languages don't even have a for loop for the same
reason (Oberon being an example). Some don't even
have any explicit loops since recursion can be used
instead - some Lisp dialects for example.

The important concept here is looping not the syntax
of the loop structure. However in Python the space
sensitive nature of the language does make a do/while
style loop a little harder to implement without breaking
good indentation practice. For example:

repeat:
     # code here
     # more code
     until condition

Now the until is visually part of the block - not nice!
(And if your school board don't understand that then
they have much bigger problems to worry about than
the syntax of loops!)

Whereas

repeat:
     # code
     # code
until condition

Is better structure but completely inconsistent with
every other Python construction (aside from try/except?)

You could of course introduce block markers to define
the block in the manner of Tcl or ruby etc but that too
would be completely inconsistent with Python style.

So instead we have the common Python idiom of

while True
    # code
    # code
    if condition: break

> repeat...until is bad practice?

No, just a variation on a theme that Python does not support
directly. There are plenty other loop structures that Python
(and other languages) does not support, for example ADAs
loop with exit:

loop:
   # code
   # code
   exit when condition
   # code
   # code
end loop

But again that can be modelled using while/break

> a langauge must have a test last structure in it to be considered.

That's a bizarre requirement that rules out many significant
languages from which important lessons can be learnt.

As a matter of interest, how would they view COBOL which
has many more loop constructs than Python but they are all
done via a single command(PERFORM) which requires the
body of the loop to be a list of functions (aka paragraphs
in COBOL).
eg

FOR loop:
PERFORM paragraph val TIMES
PERFORM paragraph VARYING variable FROM val BY val UNTIL condition.

WHILE loop
PERFORM paragraph IF condition

REPEAT
PERFORM paragraph until condition

We can also modify these case to use
WITH TEST BEFORE or WITH TEST AFTER
clauses. All very powerful but very different syntactically from
Java/Pascal/VB etc.

Also, do they have any requirements on languages to support
functional programming constructs or anonymous functions/blocks?
All very important concepts.

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 




More information about the Tutor mailing list