Python Break Statement
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:
#!/usr/bin/pythonfor "Python": # First Example if letter = = ' h ': Break print ' Current letter: ' , Letter var = ten # Second examplewhile var > 0: print ' current variable value: ', var var = var-1 If var = = 5: breakprint "Good bye!"
The result of the above instance execution:
Current letter:pcurrent letter:ycurrent letter:tcurrent variable value:10current variable value:9current variabl e value:8current variable value:7current variable value:6good bye!
Python Continue statement
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
Flow chart:
Instance:
#!/usr/bin/python#-*-coding:utf-8-*-for letter in ' python ': # First Instance if letter = = ' h ': continue print ' Current letter: ', Lettervar = ten # second instance while var > 0: var = var-1 If var = = 5: continue print ' current variable value: ', V Arprint "Good bye!"
The result of the above instance execution:
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 values: 3 current Variable Value: 2 The current variable value: + 1 Current variable value: 0G Ood bye!