Break Statement (Used to exit a loop)
Break statement is used to exit a loop before it loops all the way through.
Simply type break where you want the loop to end.
Continue Statement (Used to exit an iteration)
In some situations you might need a nested loop structure. We have previously seen that lists can be consisted of many type of different data including other lists.
Let’s see an example of nested list and nested for loop.
Continue statement makes the program skip the current iteration and return to the top of the loop. All the statements and functions after the continue statement will be neglected for that particular iteration.
Provided by HolyPython.com
breakis usually used for:
- Ending the loop prematurely.
continueis usually used for:
- Skipping the iteration and continuing with the loop.
>>> for i in range(100):
>>> if i == 5:
As you can see instead of looping all the way until 100, once i equals 5, break is executed and loop ends.
>>> for i in [“milk”, “bread”, [“cake”, “cherry”]:
>>> for j in i:
>>> if i == “cake”
As you can see continue causes to skip the current iteration and loop continues without executing the command after that particular iteration.
Advanced Concepts (Optional)
Spring at Mamquam Mountain by Nicholas Bott