Home  >  Article  >  Backend Development  >  How to determine the end of a loop body in python

How to determine the end of a loop body in python

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-06-15 17:45:235491browse

How does python determine the end of the loop body?

Related recommendations: "python video"

How to determine the end of a loop body in python

Python break statement, just like in C language, breaks the minimum closure for Or while loop.

The break statement is used to terminate the loop statement. That is, if the loop condition does not have a False condition or the sequence has not been completely recursed, the execution of the loop statement will also be stopped.

The break statement is used in while and for loops.

If you use nested loops, the break statement will stop execution of the deepest loop and start execution of the next line of code.

Python language break statement syntax:

break

Flow chart:

Example (Python 2.0)

#!/usr/bin/python
# -*- coding: UTF-8 -*-
for letter in 'Python':     # 第一个实例
   if letter == 'h':      
        break
   print '当前字母 :', letter
var = 10                    # 第二个实例
while var > 0:              
   print '当前变量值 :', var
   var = var -1
   if var == 5:   # 当变量 var 等于 5 时退出循环
      break
print "Good bye!"

Execution results of the above examples:

当前字母 : P
当前字母 : y
当前字母 : t
当前变量值 : 10
当前变量值 : 9
当前变量值 : 8
当前变量值 : 7
当前变量值 : 6
Good bye!

The above is the detailed content of How to determine the end of a loop body in python. For more information, please follow other related articles on the PHP Chinese website!

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