控制流之continue

continue语句
continue语句被用来告诉Python跳过当前循环块中的剩余语句,然后 继续 进行下一轮循环。
使用continue语句
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

例6.5 使用continue语句

#!/usr/bin/python
# Filename: continue.py
while True: 
    s = raw_input('Enter something : ') 
    if s == 'quit': 
        break 
    if len(s) < 3:
     continue
    print 'Input is of suficient lenhth'

 

它如何工作
在这个程序中,我们从用户处取得输入,但是我们仅仅当它们有至少3个字符长的时候才处理它们。所以,我们使用内建的len函数来取得长度。如果长度小于3,我们将使用continue语句忽略块中的剩余的语句。否则,这个循环中的剩余语句将被执行,我们可以在这里做我们希望的任何处理。
注意,continue语句对于for循环也有效。

posted on 2016-09-12 21:28  losbyday  阅读(199)  评论(0编辑  收藏  举报

导航