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

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:
    820
    =?ISO-8859-1?Q?Daniel_Sj=F6blom?=
    May 13, 2005
  2. asincero
    Replies:
    6
    Views:
    353
    Klaas
    Jun 25, 2007
  3. Ixiaus
    Replies:
    7
    Views:
    327
    Diez B. Roggisch
    Oct 20, 2007
  4. Andreas Waldenburger
    Replies:
    8
    Views:
    265
    birdsong
    Feb 8, 2009
  5. Mukesh
    Replies:
    4
    Views:
    605
    Paul N
    Mar 26, 2010
Loading...

Share This Page