Home > Backend Development > Python Tutorial > Where can the break statement be used? Daniel tells you the role of break in loop statements

Where can the break statement be used? Daniel tells you the role of break in loop statements

Tomorin
Release: 2018-08-13 16:20:57
Original
8428 people have browsed it

The break statement introduced in this article is like in C language, breaking the minimum closure for or while loop. 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, it will also stop executing the loop statement . break statement is used in while and for loops. If you use nested loops, the break statement stops execution of the deepest loop and starts execution of the next line of code.

The syntax of break statement :

break
Copy after login

Flow chart:

Where can the break statement be used? Daniel tells you the role of break in loop statements

Example:

#!/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!"
Copy after login

Execution result of the above example:

Current letter: P

Current letter: y

Current letter: t

Current variable value: 10

Current variable value: 9

Current variable value: 8

Current variable value: 7

Current variable Value: 6

Good bye!

Knowledge points related to this article:

What is the role of the pass statement in the Python statement? A brief discussion on the usage of pass statement

#What is the function of Python continue statement? Detailed explanation of the usage of Python continue statement


The above is the detailed content of Where can the break statement be used? Daniel tells you the role of break in loop statements. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template