Else statement executing when it shouldnt

René Klačan rene.klacan at gmail.com
Mon Jan 21 00:06:05 EST 2013


Examples:

# else branch will be executed
i = 0
while i < 5:
    i += 1
else:
    print('loop is over')


# else branch will be executed
i = 0
while i < 5:
    i += 1
    if i == 7:
        print('i == 7')
        break
else:
    print('loop is over')


# else branch wont be executed
i = 0
while i < 5:
    i += 1
    if i == 3:
        print('i == 3')
        break
else:
    print('loop is over')

On Mon, Jan 21, 2013 at 5:57 AM, eli m <techgeek201 at gmail.com> wrote:

> On Sunday, January 20, 2013 8:54:13 PM UTC-8, René Klačan wrote:
> > You have to break while loop not to execute else branch
> >
> >
> > Rene
> >
> >
> >
> Can you explain in more detail please.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130121/0f031f6b/attachment.html>


More information about the Python-list mailing list