471,312 Members | 1,802 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,312 software developers and data experts.

Noob: Loops and the 'else' construct

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!

Oct 19 '07 #1
0 808

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Ivan Shevanski | last post: by
3 posts views Thread by Sergej Andrejev | last post: by
4 posts views Thread by foker | last post: by
2 posts views Thread by Holger | last post: by
29 posts views Thread by mike_wilson1333 | last post: by
6 posts views Thread by Wijaya Edward | last post: by
19 posts views Thread by Zach Heath | last post: by
7 posts views Thread by Ixiaus | last post: by
50 posts views Thread by John Salerno | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.