May 10, 2021 Python2
The Python continue statement jumps out of the loop, and break jumps out of the loop.
The continuation statement is used to tell Python to skip the remaining statements of the current loop and proceed to the next loop.
The continue statement is used in the while and fo r loops.
The Python language continue statement syntax format is as follows:
continue
Flow chart:
Instance:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
for letter in 'Python': # 第一个实例
if letter == 'h':
continue
print '当前字母 :', letter
var = 10 # 第二个实例
while var > 0:
var = var -1
if var == 5:
continue
print '当前变量值 :', var
print "Good bye!"
The results of the above example execution:
当前字母 : P
当前字母 : y
当前字母 : t
当前字母 : o
当前字母 : n
当前变量值 : 9
当前变量值 : 8
当前变量值 : 7
当前变量值 : 6
当前变量值 : 4
当前变量值 : 3
当前变量值 : 2
当前变量值 : 1
当前变量值 : 0
Good bye!
Instance:
You can use Python's continue statement to skip some loops and print only odd numbers between 0 and 10:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
n = 0
while n < 10:
n = n + 1
if n % 2 == 0: # 如果n是偶数,执行continue语句
continue # continue语句会直接继续下一轮循环,后续的print()语句不会执行
print(n)