Introducing Python © Chirag Wazir 2002 Prev   Index   Next

while

The break statement breaks out of the smallest enclosing for or while loop.

The continue statement jumps back to the condition to continue with the next iteration of the loop.

Loop statements may have an else clause; it is executed when the loop terminates through exhaustion of the list (with for) or when the condition becomes false (with while), but not when the loop is terminated by a break statement.

# Fibonacci series:
# the sum of two elements defines the next
(a, b) = (0, 1)
while b < 1000:
    print b,
    (a, b) = (b, a+b)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987