Break and Continue: While Loops

John Gordon gordon at panix.com
Thu Jun 23 13:34:33 EDT 2016


In <639b00e0-7b9d-4ed4-96ad-6afbcd536786 at googlegroups.com> Elizabeth Weiss <cake240 at gmail.com> writes:

> i=0
> while 1==1:
>    print(i)
>    i=i+1
>    if i>=5:
>      print("Breaking")
>      break

> Why is Breaking going to be printed if i only goes up to 4?

Your code prints i and THEN adds one to it.

So i is 4, it gets printed, then 1 is added to it, so it becomes 5
and then the loop exits.

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon at panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"




More information about the Python-list mailing list