The "loop and a half"

Steve D'Aprano steve+python at pearwood.info
Tue Oct 3 23:44:09 EDT 2017


On Wed, 4 Oct 2017 09:08 am, Stefan Ram wrote:

> Steve D'Aprano <steve+python at pearwood.info> writes:
>>On Wed, 4 Oct 2017 04:45 am, Rhodri James wrote:
>>>On 03/10/17 18:29, Stefan Ram wrote:
>>>>Is this the best way to write a "loop and a half" in Python?
>>>Define "best".
>>I'd start with "define loop and a half".

[...]
>   I might got Sahami wrong, but it took it to mean this kind
>   of loop (in pseudo code, which is not necessarily Python):
> 
> while condition
>    ...
>    ...
>    ...
>    if condition
>       ...
>    end if
> end while
> 
>   The two »condition«s above are the same condition.

Hmmm. To me, that appears to be just one case of an unlimited number of
different possibilities, none more special or general than any of the others:

while condition
   ...
   if condition
      ...
   else
      ...
   end if
   ...
   if condition
      ...
   else
      ...
   end if
   ...
   if not condition
      ...
   end if
   ...
   ...
   # etc
end while


I don't see why your version is any more special, or deserving of a name, than
any other variety:

while condition
   ...
   while condition
      ...
   end while
   ...
   if not condition
       ...
   end if
   while not condition
       ...
       if not condition
           ...
       else
           ...
       end if
       ...
   end while
   ...
   while condition
       ...
   end while
   ...
end while


There is literally an infinite number of possibilities for what can go inside
a while loop. More while loops, if...else, for loops, any structure you like.
What is so special about "if condition..." that it deserves a special name or
syntax?


>   Initially, one loops through this loop an integral
>   number of times.
> 
>   But eventually the conditions is made false by the
>   upper half of the loop, so then the lower half is
>   skipped. 

Or the middle third is skipped.

Of the second and third parts of five.

Or the fifth, eighth and eleventh parts of twelve.


>   But at this point in the class, I only have 
>   shown »while« and »if« so far, iterators and for-loops
>   will only be introduced later.

Why?

Teaching while loops before for-each loops is backwards. That's one of the
bigger problems with "Learn Python the Hard Way", and I see people struggle,
really really struggle, so solve problems using a while loop which are
*trivial* using a for loop.

For-each loops are MUCH easier to understand, and should be taught first.

"Do something to each of these items" is a very natural, almost instinctive
operation. While loops are harder to write correctly, harder to reason about,
and harder to learn.

Just because for-each loops are, in some sense, derived from while loops,
doesn't mean you should teach them first.


-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list