[Edu-sig] "do" as a keyword

Brian Blais bblais at bryant.edu
Wed Dec 12 12:01:28 CET 2007


On Dec 11, 2007, at Dec 11:11:11 PM, Terry Reedy wrote:

>
> "Steven D'Aprano" <steve at REMOVE-THIS-cybersource.com.au> wrote in  
> message
> news:13lts6gb9t2j350 at corp.supernews.com...
> ||
> | But loops that run at least once is a basic element of algorithms.
> | Perhaps not as common as the zero or more times of the while  
> loop, but
> | still fundamental. It is a shame it has to be faked using:
> |
> | while True: # force the first iteration to always run
> |    process
> |    if condition: break
> |
> | Ugly and misleading.
>
> I disagree.  Nothing is being faked.  The generic loop is
>
> while True:
>     pre_process
>     if condition: break
>     post_process
>

I find that when teaching beginning programmers, they usually think  
in "until" terms, and not "while" terms.

do:
     Forward()
until Touched()

and I have to explain to them that Python doesn't have "until", and  
that the logic for while is exactly the opposite:

while not Touched():
     Forward()

they find the "while" logic to be unintuitive, and I often find  
myself feeling the same way: crafting it with the until logic, and  
then reversing it.  Perhaps I should do as above, and do:

while True:
     Forward()
     if Touched(): break

but somehow that feels wrong to me, like bypassing the point of the  
while: all that power to check for conditions, and you just use it to  
check True, and then use a break inside.  It's readable, I guess, but  
not a programming construct I am immediately drawn to.


				Brian Blais


-- 
Brian Blais
bblais at bryant.edu
http://web.bryant.edu/~bblais



-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/edu-sig/attachments/20071212/4facb263/attachment.htm 


More information about the Edu-sig mailing list