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
Flow chart:
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!"
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:
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!