Use of break and continue in Python loop statements, pythoncontinue
Python break statement
The Python break statement breaks the minimum closed for or while loop in the C language.
The break statement is used to terminate a loop statement. That is, if the loop condition does not have the False condition or the sequence is not completely recursive, the execution of the loop statement is also stopped.
The break statement is used in the while and for loops.
If you use nested loops, the break statement stops executing the deepest loop and starts executing the next line of code.
Break statement syntax in Python:
break
Flowchart:
Instance:
#!/usr/bin/pythonfor letter in 'Python': # First Example if letter == 'h': break print 'Current Letter :', letter var = 10 # Second Examplewhile var > 0: print 'Current variable value :', var var = var -1 if var == 5: breakprint "Good bye!"
Execution result of the above instance:
Current Letter : PCurrent Letter : yCurrent Letter : tCurrent variable value : 10Current variable value : 9Current variable value : 8Current variable value : 7Current variable value : 6Good bye!
Python continue statement
The Python continue statement jumps out of this loop, while the break jumps out of the entire loop.
The continue statement is used to tell Python to skip the remaining statement of the current loop and then continue the next loop.
The continue statement is used in the while and for loops.
The syntax format of the Python continue statement is as follows:
continue
Flowchart:
Instance:
#! /Usr/bin/python #-*-coding: UTF-8-*-for letter in 'python': # first instance if letter = 'H ': continue print 'current letter: ', lettervar = 10 # The Second Instance while var> 0: var = var-1 if var = 5: continue print 'current variable value :', varprint "Good bye! "
Execution result of the above instance:
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 value: 3. Current variable value: 2. Current variable value: 1. Current variable value: 0 Good bye!