In this post, we will explain the difference between the jumping statement break and continue in python.
Break-in Python
- The break is a "jump out" statement that terminates the loop that containing the break statement and move the program control to the statement that is after the loop end depending on the condition.
If the Break statement in a nested loop then it terminates the nested loop to be back to the outer loop.
Example:
Break statement in Loops
In the following example, we iterate through the "apple" string and the break statement will terminate the loop when x=l
for x in "apple":
if x== "l":
break
print(x)
The output will be
a
p
p
Continue Statement in Python
- Continue is a statement that skips only the current iteration execution of the loop.
- Continue statement is a control statement when it executed in the loop the program will skip the code that after continue and go bake to the next iteration of the loop.
Example:
In the following example, we iterate through the "apple" string and the continue statement will skip the code of the loop iteration when x=l only and continue the iteration of the loop to the end.
for x in "apple":
if x == "l":
continue
print(x)
The output will be
a
p
p
e
Conclusion
In this post, we have explored the difference between break and continue statements in Python.
See Also