Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   Loops in Python (http://www.programmingforums.org/showthread.php?t=12313)

Dietrich Jan 5th, 2007 12:45 PM

Loops in Python
 
This question came from a hijacked thread. I thought it important enough to put it in its own thread with the proper title.

What loops does Python offer and what are they used for?

I might add, that our friend Arevos gave one good answer here:
http://www.programmingforums.org/for...122207-30.html

DaWei Jan 5th, 2007 12:49 PM

I don't think you can get a much better answer than his reply. Certainly his first sentence is virtually THE definitive answer to the second question. I agree with your reason for restating it here, however.

Dietrich Jan 5th, 2007 1:56 PM

There are several ways to stop a while loop:
:

# the while loop needs a True condition, here x < 10 to run
# and will stop running on False, when x goes to 10 or above
x = 0
while x < 10:
    # do something with x
    print x
    # increment x by 1, same as x = x + 1
    x += 1

print

# 'while True' would be an endless loop, so you need to break it
# with a break condition (an if statement), stops when x is 10 or higher
x = 0
while True:
    # do something with x
    print x
    x += 1
    if x >= 10:
        break

"""
output in either case -->
0
1
2
3
4
5
6
7
8
9
"""

With the while loop you can do cute things like this:
:

# whittle down a string with while:
s = "internets"
# string s is True until it's empty
while s:
    print s
    # each time around remove one char from the end
    s = s[:-1]

"""
output -->
internets
internet
interne
intern
inter
inte
int
in
i
"""


hydroxide Jan 6th, 2007 5:09 AM

Another way of breaking a while loop - or even nested while loops -- is to use a flag. If you find yourself using this technique, though, you probably need to redesign:
:

flag1 = False
flag2 = False
while not (flag1 or flag2):
    for i in range(10):
        print i
        if i == 4:
            flag1 = True
            break
        elif i == 6:
            flag2 = True
            break


A sometimes useful trick for getting out of nested loops is to raise an exception - StopIteration is the clearest, (use sparingly - a redesign/restructure is normally better):
:

def myfunc(i, j):
    print i, j
    if i == j == 1:
        raise StopIteration
   
try:
    while True:
        for i in range(10):
            for j in range(10):
                myfunc(i, j)
except StopIteration:
    pass

-T.

Dietrich Jan 6th, 2007 11:34 AM

Here is an example of using a recursive function as a while loop with a name:
:

def count_to_9(x):
    # do something with x
    print x
    x += 1
    if x >= 10:
        return
    count_to_9(x)

# start the loop at zero
count_to_9(0)

"""
output -->
0
1
2
3
4
5
6
7
8
9
"""

Note, this is just an example. Function calls are too time expensive to make this practical.

If you ever have to break out of some badly nested loops, put them into a function and use return to break out.


All times are GMT -5. The time now is 10:12 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC