Break & Continue Keywords
- If you want to stop or come out of the loop, you can use the break keyword. This keyword helps you come out of both for and while loops.
- Let's say you have a list of numbers, numbers = [1, 3, 2, 5, 4, 6, 8]. Now you are interested in getting the first even number.
For Loop & break Keyword
You were only interested in the first even number, but the above Python code has returned all the even numbers in the list.
Let's see how the break keyword can help:
- The first even number in the list is 2. Once the condition is satisfied for the first time, we print the number and use the break keyword to come out of the loop.
While Loop & break Keyword
- As you can observe, the while loop was to be run till the value of i was less than 9, but here the output is only 1, 2, and 3.
- This is because when the value of i was 3, the loop was discontinued, i.e., the break keyword helped to break the while loop.
- The continue keyword helps you skip or end the current iteration of both for and while loops and starts the next iteration. It will be more apparent through the examples below.
- For example, consider the list of numbers, numbers = [1, 3, 2, 5, 4, 6, 8]. You want to skip the iteration if the current number is an even number, otherwise, print the number.