Noob: Loops and the 'else' construct

Discussion in 'Python' started by Ixiaus, Oct 19, 2007.

  1. Ixiaus

    Ixiaus Guest

    I have just come across a site that discusses Python's 'for' and
    'while' loops as having an (optional) 'else' structure.

    At first glance I interpreted it as being a bit like the 'default'
    structure in PHP's switch block... But the switch block isn't a loop,
    so, I am now confused as to the reason for using 'else' with the for
    and while loops...

    A few quick tests basically show that statements in the else structure
    are executed at the fulfillment of the loop's expression (ie, no
    break).

    Example:

    for i in range(10):
    print i
    else:
    print 'the end!'

    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    the end!
    Ixiaus, Oct 19, 2007
    #1
    1. Advertising

  2. En Thu, 18 Oct 2007 23:44:27 -0300, Ixiaus <>
    escribió:

    > I have just come across a site that discusses Python's 'for' and
    > 'while' loops as having an (optional) 'else' structure.
    >
    > At first glance I interpreted it as being a bit like the 'default'
    > structure in PHP's switch block... But the switch block isn't a loop,
    > so, I am now confused as to the reason for using 'else' with the for
    > and while loops...
    >
    > A few quick tests basically show that statements in the else structure
    > are executed at the fulfillment of the loop's expression (ie, no
    > break).


    A `while` loop tests a condition: if it evaluates to true, keep cycling;
    if it is false, stop. The `else` clause is executed when the condition is
    false, as in any `if` statement. If you exit the loop by using `break`,
    the `else` part is not executed (because you didn't get out of the loop by
    determining the condition falseness)

    You can think of a `for` loop as meaning `while there are remaining
    elements to be iterated, keep cycling` and the `else` clause applies when
    there are no more elements. A `break` statement does not trigger the else
    clause because the iteration was not exhausted.

    Once you get the idea, it's very simple.

    --
    Gabriel Genellina
    Gabriel Genellina, Oct 19, 2007
    #2
    1. Advertising

  3. * Gabriel Genellina (Fri, 19 Oct 2007 00:11:18 -0300)
    > En Thu, 18 Oct 2007 23:44:27 -0300, Ixiaus <>
    > escribió:
    > > I have just come across a site that discusses Python's 'for' and
    > > 'while' loops as having an (optional) 'else' structure.
    > >
    > > At first glance I interpreted it as being a bit like the 'default'
    > > structure in PHP's switch block... But the switch block isn't a loop,
    > > so, I am now confused as to the reason for using 'else' with the for
    > > and while loops...
    > >
    > > A few quick tests basically show that statements in the else structure
    > > are executed at the fulfillment of the loop's expression (ie, no
    > > break).

    >
    > A `while` loop tests a condition: if it evaluates to true, keep cycling;
    > if it is false, stop. The `else` clause is executed when the condition is
    > false, as in any `if` statement. If you exit the loop by using `break`,
    > the `else` part is not executed (because you didn't get out of the loop by
    > determining the condition falseness)


    So a for/else loop is exactly the same thing as a for loop with the
    else clause outside the loop (except for "break")? Guess that's why I
    never used that...

    Thorsten
    Thorsten Kampe, Oct 19, 2007
    #3
  4. Ixiaus

    Dustan Guest

    On Oct 19, 3:12 am, Thorsten Kampe <> wrote:
    > So a for/else loop is exactly the same thing as a for loop with the
    > else clause outside the loop (except for "break")?


    Am I missing something here? It sounds to me like you just described
    two identical constructs.

    > Guess that's why I
    > never used that...
    >
    > Thorsten
    Dustan, Oct 19, 2007
    #4
  5. Ixiaus

    Paul Boddie Guest

    On 19 Okt, 13:39, Dustan <> wrote:
    > On Oct 19, 3:12 am, Thorsten Kampe <> wrote:
    >
    > > So a for/else loop is exactly the same thing as a for loop with the
    > > else clause outside the loop (except for "break")?

    >
    > Am I missing something here? It sounds to me like you just described
    > two identical constructs.


    Think of the loop-plus-else construct as behaving like this:

    while 1:
    # Get next element (in a for loop)
    if loop_condition: # eg. whether we have an element
    # Loop body statement
    else:
    # Loop else statement
    break

    Taking the example...

    for i in range(10):
    print i
    else:
    print 'the end!'

    This is equivalent to...

    while 1:
    # Get next element (from the range iterator)
    if next element: # yes, it's more complicated than this
    print i
    else:
    print 'the end!'
    break

    Now consider what happens if you put a break statement inside the for
    loop.

    Paul
    Paul Boddie, Oct 19, 2007
    #5
  6. Ixiaus

    MRAB Guest

    On Oct 19, 4:11 am, "Gabriel Genellina" <>
    wrote:
    > En Thu, 18 Oct 2007 23:44:27 -0300, Ixiaus <>
    > escribió:
    >
    > > I have just come across a site that discusses Python's 'for' and
    > > 'while' loops as having an (optional) 'else' structure.

    >
    > > At first glance I interpreted it as being a bit like the 'default'
    > > structure in PHP's switch block... But the switch block isn't a loop,
    > > so, I am now confused as to the reason for using 'else' with the for
    > > and while loops...

    >
    > > A few quick tests basically show that statements in the else structure
    > > are executed at the fulfillment of the loop's expression (ie, no
    > > break).

    >
    > A `while` loop tests a condition: if it evaluates to true, keep cycling;
    > if it is false, stop. The `else` clause is executed when the condition is
    > false, as in any `if` statement. If you exit the loop by using `break`,
    > the `else` part is not executed (because you didn't get out of the loop by
    > determining the condition falseness)
    >
    > You can think of a `for` loop as meaning `while there are remaining
    > elements to be iterated, keep cycling` and the `else` clause applies when
    > there are no more elements. A `break` statement does not trigger the else
    > clause because the iteration was not exhausted.
    >
    > Once you get the idea, it's very simple.
    >

    It's useful when you want to search for an item and to do something if
    you don't find it, eg:

    for i in items:
    if is_wanted(i):
    print "Found it"
    break
    else:
    print "Didn't find ir"
    MRAB, Oct 20, 2007
    #6
  7. * Dustan (Fri, 19 Oct 2007 11:39:04 -0000)
    > On Oct 19, 3:12 am, Thorsten Kampe <> wrote:
    > > So a for/else loop is exactly the same thing as a for loop with the
    > > else clause outside the loop (except for "break")?

    >
    > Am I missing something here? It sounds to me like you just described
    > two identical constructs.


    #
    for i in range(10):
    print i
    else:
    print 'the end!'
    #

    is the same else

    #
    for i in range(10):
    print i
    print 'the end!'
    #
    Thorsten Kampe, Oct 20, 2007
    #7
  8. MRAB schrieb:
    > On Oct 19, 4:11 am, "Gabriel Genellina" <>
    > wrote:
    >> En Thu, 18 Oct 2007 23:44:27 -0300, Ixiaus <>
    >> escribió:
    >>
    >>> I have just come across a site that discusses Python's 'for' and
    >>> 'while' loops as having an (optional) 'else' structure.
    >>> At first glance I interpreted it as being a bit like the 'default'
    >>> structure in PHP's switch block... But the switch block isn't a loop,
    >>> so, I am now confused as to the reason for using 'else' with the for
    >>> and while loops...
    >>> A few quick tests basically show that statements in the else structure
    >>> are executed at the fulfillment of the loop's expression (ie, no
    >>> break).

    >> A `while` loop tests a condition: if it evaluates to true, keep cycling;
    >> if it is false, stop. The `else` clause is executed when the condition is
    >> false, as in any `if` statement. If you exit the loop by using `break`,
    >> the `else` part is not executed (because you didn't get out of the loop by
    >> determining the condition falseness)
    >>
    >> You can think of a `for` loop as meaning `while there are remaining
    >> elements to be iterated, keep cycling` and the `else` clause applies when
    >> there are no more elements. A `break` statement does not trigger the else
    >> clause because the iteration was not exhausted.
    >>
    >> Once you get the idea, it's very simple.
    >>

    > It's useful when you want to search for an item and to do something if
    > you don't find it, eg:
    >
    > for i in items:
    > if is_wanted(i):
    > print "Found it"
    > break
    > else:
    > print "Didn't find ir"


    Wrong. It's not:

    for i in []:
    print i
    else:
    print "I'm reached, too"

    prints out "I'm reached, too"

    The else will ONLY not get executed when the loop is left prematurely
    through a break:

    for i in [1]:
    print i
    break
    else:
    print "I'm reached, too"

    won't print the "I'm ..."


    Diez
    Diez B. Roggisch, Oct 20, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas Zangl

    [BCEL] If then else construct - again

    Thomas Zangl, May 13, 2005, in forum: Java
    Replies:
    4
    Views:
    822
    =?ISO-8859-1?Q?Daniel_Sj=F6blom?=
    May 13, 2005
  2. asincero
    Replies:
    6
    Views:
    353
    Klaas
    Jun 25, 2007
  3. Ixiaus
    Replies:
    0
    Views:
    243
    Ixiaus
    Oct 19, 2007
  4. Andreas Waldenburger
    Replies:
    8
    Views:
    266
    birdsong
    Feb 8, 2009
  5. Mukesh
    Replies:
    4
    Views:
    606
    Paul N
    Mar 26, 2010
Loading...

Share This Page