,就像在C语言中,打破了最小封闭for或while循环。 而continue语句,终止这次循环,继续执行下一次迭代的循环。
for和while循环可以设置一个else子句;如果你的循环不是通过break终止的话,最后会执行else语句的内容。比如搜索素数的例子:
>>> for n in range(2, 10):
... for x in range(2, n):
... if n % x == 0:
... print n, 'equals', x, '*', n/x
... break
... else:
... # loop fell through without finding a factor
... print n, 'is a prime number'
...
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3
阅读(700) | 评论(0) | 转发(0) |