while semantics

David Bear david.bear at asu.edu
Thu Mar 13 20:52:28 EST 2003


Grant Edwards wrote:

> 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.
> 

What about

while (condition):
        do stuff
        do more stuff
        condition becomes true
        do even more stuff

Will while terminate AS SOON as condition is true, or will it evaluate all 
statements in the block?




More information about the Python-list mailing list