当前位置: 首页 > 文档资料 > 简明 Python 教程 >

控制流 / continue语句

优质
小牛编辑
134浏览
2023-12-01

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

使用continue语句

例6.5 使用continue语句

#!/usr/bin/python
# Filename: continue.py


whileTrue:
    s =raw_input('Enter something : ')
    ifs =='quit':
        break
    iflen(s) <3:
        continue
    print'Input is of sufficient length'
    # Do other kinds of processing here...

(源文件:code/continue.py)

输出

$ python continue.py
Enter something : a
Enter something : 12
Enter something : abc
Input is of sufficient length
Enter something : quit

它如何工作

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

注意,continue语句对于for循环也有效。