跳至内容

Python continue继续

Python中,continue语句用于跳过本次循环,直接进入下一次循环。

语法

python
continue
continue

continue 工作方式

flow-continue

例 1:for循环中的continue语句

python
for index in range(5):
    if index== 2
        continue
    print(index)


# 输出:
# 0
# 1
# 3
# 4
for index in range(5):
    if index== 2
        continue
    print(index)


# 输出:
# 0
# 1
# 3
# 4

例 2:while循环中的continue语句

python
num = 1
while num < 5:
    print(num)

    if (i%3) == 0:
        continue

    i = i+1


# 输出:
# 1
# 2
# 4
num = 1
while num < 5:
    print(num)

    if (i%3) == 0:
        continue

    i = i+1


# 输出:
# 1
# 2
# 4