Difference Between Break and Continue Python
The difference between break, continue, exit() and pass in python
1. Break: break out of the loop and no longer execute
- The Python break statement, just like in the C language, breaks the smallest closed for or while loop.
- The 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.
- The break statement is used in while and for loops.
- If you use nested loops, the break statement will stop the deepest loop and start executing the next line of code.
###Example 1: break out of the infinite while loop >```python while True: print("123") break print("456") code> ``` ___ ###Example 2: break is to terminate this loop. For example, if you have many for loops, you are in one of them A break is written in the for loop. If the conditions are met, only the loop in the for will be terminated, and the program will jump to the previous for loop and continue to go down >```python for i in range(5): print("-----%d-----" %i) for j in range(5): if j> 4: break print (j) ```
When j>5 is encountered here, the second level of for will not loop, and continue to jump to the previous level of loop
$ py break.py -----0----- 0 1 2 3 -----1----- 0 1 2 3 -----2----- 0 1 2 3 -----3----- 0 1 2 3
2. continue: jump out of this loop and execute the next time
- Python continue statement jumps out of this loop, and break jumps out of the entire loop.
- The continue statement is used to tell Python to skip the remaining statements in the current loop, and then continue to the next loop.
- The continue statement is used in while and for loops.
Example 1: continue is triggered by if judgment, jumps out of the current level of for loop, terminates the output of'h', and continues to the next for.
for letter in 'Python':
if letter == 'h':
continue #Here jumps out of the loop of for enumeration'h'
print('Current letter:', letter)
#There is no'h' in the output result
$ py continue.py
Current letter: P
Current letter: y
Current letter: t
Current letter: o
Current letter: n
##Example 2: >```python var = 5 while var > 0: var = var -1 if var == 3: continue print('Current variable value:', var) print("Good bye!") $ py continue.py Current variable value: 4 Current variable value: 2 Current variable value: 1 Current variable value: 0 Good bye!``` ###If continue is replaced by break >```python var = 5 while var > 0: var = var -1 if var == 3: break print('Current variable value:', var) print("Good bye!") $ py continue.py Current variable value: 4 Good bye!
Example 3: When the loop reaches this point, continue to perform some operations here. After the execution is complete, continue to perform what needs to be done in this layer of loop that meets the conditions, and will not terminate this layer of loop (only jump out of this loop) once).
Modify the break example above:
for i in range(10):
print("-----%d-----" %i)
for j in range(10):
if j > 5 and j <= 8:
print("I am continue special")
continue
print(j)
The loop here will print out what needs to be done between 5 and 8, but it will not terminate the second loop. If the number between 5 and 8 is not met, it will continue to loop what needs to be done. #3, exit(): end the entire program >```python for element in "Python": if element == "t": exit() else: print(element)
4. Pass: don't do anything, only play the role of placeholder
for element in "Python":
if element == "y":
pass
else:
print(element)
The difference between break and continue
break terminates the current loop: break ---- must be used in a loop break ---- Terminate the current loop and the code below the break will not be executed continue Jump out of this loop and continue...
The difference between break and continue
Break can leave the current block of switch, for, while, do while, and advance to the next statement after the block. In switch, it is mainly used to interrupt the comparison of the next case. In for,...
The difference between continue and break
Break is to jump out of the current loop (not the entire loop) E.g: The printed effect is like this: Continue is to jump out of this loop and execute the next loop can write such a co...
The difference between break and continue
(1) break jumps out of the innermost loop and terminates (2) continue just terminates this cycle, and then executes the following cycle sum=6; Execute break End of cycle ...
The difference between Break and Continue
BREAK: Force withdraw from the entire cycle, not perform the remaining cycle statements Continue: Forced termination of this cycle and continue to execute the remaining cycle statements ...
Python loop-break and continue
break is used to completely end a loop, jump out of the loop body, and execute the statement following the loop operation result continue is used to terminate this cycle and continue to the next cycle...
babbagephompecture.blogspot.com
Source: https://www.programmerall.com/article/617174102/