while semantics

Grant Edwards grante at visi.com
Fri Mar 14 01:26:42 CET 2003

In article <b4r6eh$oj4$2 at news.asu.edu>, David Bear wrote:
> I think I'm confusing myself regarding semantics for a while loop.
> while (conditionX):
>    do something
>     while (conditionY):
>        do something else
>           while (conditionZ):
>                do something
>                here conditionX becomes False
>                print something
>           print something else
> Will the outer while loop cause all nested whiles to end as soon as 
> conditionX is false?

No.  conditionX will only be checked once per "outer loop".  If
you're in one of the inner loops, you stay there until that
loop's condition is false.

> or will 'print something' be reached, then 'print 
> something else' and so on untill it reaches the outer scope?

Neither.  You'll stay inside in Z loop until conditionZ
becomes false.  Then you'll stay in the Y loop until conditionY
becomes false.  After both the Z and Y loops have terminated,
conditionX will be checked again.

Grant Edwards                   grante             Yow!  TATTOOED MIDGETS
                                  at               are using ALFREDO in their
                               visi.com            SALAMI FACTORY!

More information about the Python-list mailing list