Python Crash Course for Beginners | Break and Continue in Python
Controlling the flow of a program is an essential aspect of programming. In Python, two important statements that allow you to manage the flow of loops are ‘break’ and ‘continue’. These statements can be used in both for loops and while loops, enabling you to either exit the loop prematurely or skip a portion of the loop under specific conditions. In this article, we will explore the Python break and continue statements, providing coding examples and explanations to help you understand their functionality and usage.
Python break Statement
The ‘break’ statement in Python is used to exit a loop prematurely, terminating the current iteration and skipping any remaining iterations. The execution of the loop stops immediately, and the program continues with the code following the loop.
Example 1: Using break in a for Loop
Let’s start with an example that demonstrates the use of the ‘break’ statement in a for loop.
for number in range(1, 11):
if number == 5:
break
print(number)
In this example, we use a for loop to iterate through a range of numbers from 1 to 10. If the current number is equal to 5, the ‘break’ statement is executed, terminating the loop. The output will be:
1
2
3
4
Notice that the numbers 5 to 10 are not printed, as the loop terminates as soon as the ‘break’ statement is encountered.
Example 2: Using break in a while Loop
Now let’s look at an example that demonstrates the use of the ‘break’ statement in a while loop.
count = 0
while count < 10:
count += 1
if count == 5:
break
print(count)
In this example, we use a while loop to check if the variable ‘count’ is less than 10. Inside the loop, we increment ‘count’ by 1. If ‘count’ is equal to 5, the ‘break’ statement is executed, terminating the loop. The output will be:
1
2
3
4
Similar to the previous example, the numbers 5 to 10 are not printed due to the early termination of the loop.
Python continue Statement
The ‘continue’ statement in Python is used to skip the remaining code in the current iteration of a loop and proceed to the next iteration. Unlike the ‘break’ statement, which completely exits the loop, the ‘continue’ statement only skips the current iteration and allows the loop to continue.
Example 3: Using continue in a for Loop
Let’s look at an example that demonstrates the use of the ‘continue’ statement in a for loop.
for number in range(1, 11):
if number % 2 == 0:
continue
print(number)
In this example, we use a for loop to iterate through a range of numbers from 1 to 10. If the current number is an even number (i.e., divisible by 2), the ‘continue’ statement is executed, skipping the print statement and proceeding to the next iteration. The output will be:
1
3
5
7
9
Only odd numbers are printed, as the ‘continue’ statement skips the even numbers in the loop.
Example 4: Using continue in a while Loop
Now let’s examine an example that demonstrates the use of the ‘continue’ statement in a while loop.
count = 0
while count < 10:
count += 1
if count % 3 == 0:
continue
print(count)
In this example, we use a while loop to check if the variable ‘count’ is less than 10. Inside the loop, we increment ‘count’ by 1. If ‘count’ is divisible by 3, the ‘continue’ statement is executed, skipping the print statement and proceeding to the next iteration. The output will be:
1
2
4
5
7
8
10
Only the numbers not divisible by 3 are printed, as the ‘continue’ statement skips the iterations where ‘count’ is divisible by 3.
Summary
The Python break and continue statements are powerful tools for controlling the flow of loops in your programs. By understanding and mastering the usage of these statements, you can create more efficient and versatile applications that respond effectively to different conditions.
In this article, we explored the Python break and continue statements with examples and explanations to help you comprehend their functionality and usage. With practice, you’ll be able to apply these concepts in your own projects, enhancing your ability to work with loops and manage program flow.
Latest end-to-end Learn by Coding Projects (Jupyter Notebooks) in Python and R:
All Notebooks in One Bundle: Data Science Recipes and Examples in Python & R.
End-to-End Python Machine Learning Recipes & Examples.
End-to-End R Machine Learning Recipes & Examples.
Applied Statistics with R for Beginners and Business Professionals
Data Science and Machine Learning Projects in Python: Tabular Data Analytics
Data Science and Machine Learning Projects in R: Tabular Data Analytics
Python Machine Learning & Data Science Recipes: Learn by Coding
R Machine Learning & Data Science Recipes: Learn by Coding
Comparing Different Machine Learning Algorithms in Python for Classification (FREE)
There are 2000+ End-to-End Python & R Notebooks are available to build Professional Portfolio as a Data Scientist and/or Machine Learning Specialist. All Notebooks are only $29.95. We would like to request you to have a look at the website for FREE the end-to-end notebooks, and then decide whether you would like to purchase or not.