Python Crash Course for Beginners | Break and Continue in Python

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.

 

Personal Career & Learning Guide for Data Analyst, Data Engineer and Data Scientist

Applied Machine Learning & Data Science Projects and Coding Recipes for Beginners

A list of FREE programming examples together with eTutorials & eBooks @ SETScholars

95% Discount on “Projects & Recipes, tutorials, ebooks”

Projects and Coding Recipes, eTutorials and eBooks: The best All-in-One resources for Data Analyst, Data Scientist, Machine Learning Engineer and Software Developer

Topics included: Classification, Clustering, Regression, Forecasting, Algorithms, Data Structures, Data Analytics & Data Science, Deep Learning, Machine Learning, Programming Languages and Software Tools & Packages.
(Discount is valid for limited time only)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.

Learn by Coding: Tutorials on Applied Machine Learning and Data Science for Beginners

Please do not waste your valuable time by watching videos, rather use end-to-end (Python and R) recipes from Professional Data Scientists to practice coding, and land the most demandable jobs in the fields of Predictive analytics & AI (Machine Learning and Data Science).

The objective is to guide the developers & analysts to “Learn how to Code” for Applied AI using end-to-end coding solutions, and unlock the world of opportunities!