Home >Backend Development >Python Tutorial >Python中Continue语句的用法的举例详解

Python中Continue语句的用法的举例详解

WBOY
WBOYOriginal
2016-06-06 11:16:161598browse

 Python continue语句返回while循环的开始。Continue语句拒绝在该循环的当前迭代中的其余语句执行并移动控制返回到循环的顶部(开始位置)。

continue语句可以在while和for循环使用。
语法

Python continue语句的语法如下:

continue
流程图:

2015514154004423.jpg (285×331)

 例子

#!/usr/bin/python

for letter in 'Python':   # First Example
  if letter == 'h':
   continue
  print 'Current Letter :', letter

var = 10          # Second Example
while var > 0:       
  var = var -1
  if var == 5:
   continue
  print 'Current variable value :', var
print "Good bye!"

当执行上面的代码,产生以下结果:

Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : o
Current Letter : n
Current variable value : 9
Current variable value : 8
Current variable value : 7
Current variable value : 6
Current variable value : 4
Current variable value : 3
Current variable value : 2
Current variable value : 1
Current variable value : 0
Good bye!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn