How to Use Break and Continue Statements in Python?

This tutorial explores the strategic use of Python break and continue statements to take your programming skills to the next level. | ProjectPro

Python provides several control flow statements to enhance the efficiency of your code. Among them, the Python break and continue statements play a crucial role in managing loops. This tutorial will help you understand the workings of these statements, explore their applications, and understand how they can be used to control the flow within loops.

What does Break do in Python? 

The break statement in Python is used to exit a loop prematurely. When encountered, it immediately terminates the innermost loop, regardless of the loop condition. This can be particularly useful when a specific condition is met, and you want to exit the loop early. 

What does Continue do in Python? 

On the other hand, the continue statement skips the rest of the code inside a loop and proceeds to the next iteration. It's handy when you want to bypass certain iterations based on a specific condition, allowing you to control the loop's flow more effectively.

How to Use Continue in Python? 

Using the continue statement is straightforward. When the interpreter encounters it, it jumps to the next iteration of the loop. Here is a basic syntax to illustrate its usage:

Basic Syntax - 

Example - How to Use Continue within a Loop in Python? 

This code snippet skips the print statement for even numbers, demonstrating how continue alters the flow of the loop.

How to Use Break in Python? 

The break statement is a control flow keyword used to exit a loop prematurely. It is typically employed within loops, such as for or while loops, to terminate the loop execution when a certain condition is met. When the break statement is encountered, the program immediately exits the loop, and the control is transferred to the next statement following the loop. This can be useful when stopping the loop execution based on a specific condition rather than completing the entire iteration. The break statement provides a flexible way to manage the flow of your code, allowing you to control loop execution efficiently based on dynamic conditions.

Basic Syntax - 

Example - How to Break a While Loop in Python? 

Here is a simple example to break out of a while loop in Python - 

In this case, the loop will terminate when count reaches 3.

Example - How to Break out of a For Loop in Python? 

Here is a simple example of breaking out of a while loop in Python - 

This code snippet exits the loop when the letter 'h' is encountered.

Example - How to Line Break in Python? 

You can create a line break in a string using the escape character \n. Here's a short example:

Here, the \n character is used to introduce a line break, separating the string into two lines when printed. This demonstrates how you can easily include line breaks within strings to format text or output in a way that is visually appealing or more readable. 

Python Break vs Continue: Difference Between Break and Continue in Python

break terminates the loop prematurely, while continue skips the remaining code in the current iteration and moves on to the next one. Check the table below to understand the key differences between both based on certain parameters: 

Feature 

Break Statement 

Continue Statement 

Functionality 

Terminates the entire loop permanently 

Skips the rest of the code in the iteration and moves to the next one.

Scope 

Affects the innermost loop enclosing it.

Impacts only the current iteration within the loop.

Effect on Loop Flow 

Abruptly ends the loop.

Skips the remaining code in the current iteration and proceeds to the next one.

Example - How to Use CONTINUE and BREAK Statements within a Loop in Python.

Step 1 - Importing Library

import random

We have only imported random which is needed.

Step 2 - Creating a loop

We have created a while loop after creating an object. We have made conditions to keep the loop working and to stop the loop. We can stop the loop by using the break function.

running = True while running: s = random.randint(0,5) if s < 3: print(s, ": It is too small, starting again.") continue if s == 4: running = False print("It is 4! Changing running to false") if s == 5: print("It is 5! Breaking Loop!") break

We get the output as:

0 : It is too small, starting again.

2 : It is too small, starting again.

It is 5! Breaking Loop!

Get Your Hands-on Python Projects with ProjectPro! 

Learning about break and continue statements in Python is valuable for any programmer. These statements provide the flexibility needed to control the flow within loops efficiently. Whether prematurely exiting a loop with a break or skipping iterations with continue, these statements empower you to write more concise and practical code. However, true magic happens when theoretical knowledge is put into practice. This is where ProjectPro comes in. With over 270+ projects focused on data science and big data, ProjectPro transforms learning into a dynamic, hands-on experience. Getting practical experience through real-world projects is critical to becoming a proficient Python developer, and ProjectPro is the ideal platform for honing those skills. So, start your practical learning journey, explore diverse Python projects, and elevate your Python proficiency with ProjectPro! 

Download Materials

What Users are saying..

profile image

Ray han

Tech Leader | Stanford / Yale University
linkedin profile url

I think that they are fantastic. I attended Yale and Stanford and have worked at Honeywell,Oracle, and Arthur Andersen(Accenture) in the US. I have taken Big Data and Hadoop,NoSQL, Spark, Hadoop... Read More

Relevant Projects

Build CNN Image Classification Models for Real Time Prediction
Image Classification Project to build a CNN model in Python that can classify images into social security cards, driving licenses, and other key identity information.

Deploying Machine Learning Models with Flask for Beginners
In this MLOps on GCP project you will learn to deploy a sales forecasting ML Model using Flask.

LLM Project to Build and Fine Tune a Large Language Model
In this LLM project for beginners, you will learn to build a knowledge-grounded chatbot using LLM's and learn how to fine tune it.

Build Regression Models in Python for House Price Prediction
In this Machine Learning Regression project, you will build and evaluate various regression models in Python for house price prediction.

Azure Deep Learning-Deploy RNN CNN models for TimeSeries
In this Azure MLOps Project, you will learn to perform docker-based deployment of RNN and CNN Models for Time Series Forecasting on Azure Cloud.

End-to-End Snowflake Healthcare Analytics Project on AWS-2
In this AWS Snowflake project, you will build an end to end retraining pipeline by checking Data and Model Drift and learn how to redeploy the model if needed

PyTorch Project to Build a LSTM Text Classification Model
In this PyTorch Project you will learn how to build an LSTM Text Classification model for Classifying the Reviews of an App .

NLP and Deep Learning For Fake News Classification in Python
In this project you will use Python to implement various machine learning methods( RNN, LSTM, GRU) for fake news classification.

Build Real Estate Price Prediction Model with NLP and FastAPI
In this Real Estate Price Prediction Project, you will learn to build a real estate price prediction machine learning model and deploy it on Heroku using FastAPI Framework.

Build a Churn Prediction Model using Ensemble Learning
Learn how to build ensemble machine learning models like Random Forest, Adaboost, and Gradient Boosting for Customer Churn Prediction using Python