The Python break statement, like in the C language, breaks the minimum enclosing for or while loop. The break statement terminates the Loop statement, that is, the loop condition does not have a false condition, or the sequence is not fully recursive, and the loop statement is stopped. The break statement is used in the while and for loops. If you use a nested loop, the break statement stops executing the deepest loop and starts executing the next line of code. Python language break statement syntax: Break
Flow chart
instance (Python2.0+)#!/usr/bin/python#-*-coding:utf-8-*- forLetterinch 'Python': # First InstanceifLetter = ='h': Break Print'Current Letter:', Letter Var=Ten# A second instance whilevar >0: Print'Current variable Value:', var var= var-1 ifvar = =5: # when variable var equals5when exiting loop break print"Good bye!"result of the above instance execution: Current Letter: P Current Letter: y current letter: T current variable value:TenCurrent variable Value:9Current variable Value:8Current variable Value:7Current variable Value:6Good bye!
The Python continue statement jumps out of the loop, and break jumps out of the loop. The continue statement is used to tell Python to skip the remaining statements of the current loop, and then proceed to the next round of loops. The continue statement is used in the while and for loops. The Python language continue statement syntax is formatted as follows: Continue flowchart: cpp_continue_statement instance: instance (Python2.0+)#!/usr/bin/python#-*-coding:utf-8-*- forLetterinch 'Python': # First InstanceifLetter = ='h': Continue print'Current Letter:', Letter Var=Ten# A second instance whilevar >0: Var= var-1 ifvar = =5: Continue print'Current variable Value:', Varprint"Good bye!"result of the above instance execution: Current Letter: P Current Letter: y current letter: T current letter: o Current letter: N Current variable value:9Current variable Value:8Current variable Value:7Current variable Value:6Current variable Value:4Current variable Value:3Current variable Value:2Current variable Value:1Current variable Value:0Good bye!
The Python Pass is an empty statement in order to maintain the integrity of the program structure. Pass does not do anything, generally used as a placeholder statement. The Python language Pass statement has the following syntax format: pass instance: #!/usr/bin/python#-*-coding:utf-8-*-# output Python for each letter forLetterinch 'Python': ifLetter = ='h': Pass Print'This is a pass block .'Print'Current Letter:', Letterprint"Good bye!"result of the above instance execution: Current Letter: P Current Letter: y current Letter: t This is the pass block current letter: H current Letter: o Current letter: Ngood bye!
Python break, continue and pass statements (eight)