Loops are a fundamental concept in programming, whether they are for, while, or do-while loops. They enable repetitive tasks to be done in fewer lines of code. While loops iterate over a block of code as long as the loop condition remains true. For loops iterate over a sequence, such as a list or tuple, and enables the coder to execute code against all items in a sequence.
But sometimes, it is necessary to break out of a loop early, even if the loop condition hasn’t been met or the items have not been processed in the sequence. When that’s the case, the break
keyword becomes useful. In this article, we’ll dive into the usage of the break
keyword in Python.
Breaking out of a for loop
The break
keyword is useful for short-circuiting a for loop. This can be useful if you want to exit early out of a loop once a certain condition has been met. Here is an example that demonstrates this point:
# program to print even numbers nums=[1,2,3,4,5,6,7,8,9,10] for num in nums: if num % 2 == 0: print(num, 'is an even number') else: print('breaking loop as we only need even numbers') break
Here is the output from the code snippet above:
breaking loop as we only need even numbers 1 is an odd number
As you can see from the code snippet above, the break
keyword is used to stop the for loop as soon as it encounters the first odd number. The output shows that the loop was interrupted and only the even numbers were printed. Notice how 1 is an odd number
is printed after the loop ends; this shows that only the even digits were processed through the loop.
Breaking out of a while loop
Similar to a for loop, you can also use the break
keyword to break out of a while loop. It is to be noted that using the break
keyword is necessary to avoid an infinite loop when coding with a while loop. Here is an example:
# program to count to 10 num = 0 while num <= 10: if num == 10: print('<<Count complete') break print(num) num += 1
You should get the following output when you execute the above code snippet:
0 1 2 3 4 5 6 7 8 9 <<Count complete
In this example, the break
keyword is used to stop the loop when num
is 10. In this way, it skips the infinite loop problem entirely.
Using the break keyword in nested loops
Nested loops are common when programming. A nested loop is, to be precise, a loop inside of another loop. You can go through several iterations and process data with multiple levels of analysis, allowing you to handle complex data analysis tasks quickly. In this instance, using the break
keyword becomes quite handy. Let’s see how it works with nested loops:
# program to skip some numbers for i in range(1,6): for j in range(1,11): if j == 3: continue elif j == 7: print('Exiting program as now j = 7') break else: print(i,j)
In this example, there are two nested loops iterating over a range of numbers. Once j equals 3 inside the inner loop, the continue
keyword is used to skip over the rest of the statements in the inner loop, headed back to the start of the inner loop, and therefore skipping the execution of print(i,j). This is why we see that 3 is skipped over in the output. Later, when j
equals 7 and the innermost loop is exited by the break
keyword, the outer loop is restarted after j
equals 7 since we see that print(i,j) is not executed when j
equals 7 in that specific case.
Here’s what you can expect from the above output:
1 1 1 2 1 4 1 5 1 6 Exiting program as now j = 7 2 1 2 2 2 4 2 5 2 6 2 8 2 9 3 1 3 2 3 4 3 5 3 6 3 8 3 9 4 1 4 2 4 4 4 5 4 6 4 8 4 9 5 1 5 2 5 4 5 5 5 6 5 8 5 9
Conclusion
The break
keyword is an essential feature of the Python programming language to improve efficacy and streamline code. Its versatility and ease-of-use make it valuable to coders. With its capability to short-circuit all loops, it makes skipping over needless data or variables very easy. As demonstrated above, it’s simple to use with for loops, while loops, and even nested loops.