Break and Continue in Python are used to alter the flow of ongoing loops where breaks end the loop completely and step out while continue just skip one given iteration in the loop. Both statements need a condition which must be met to execute the break and continue in Python.
In this blog, we will learn about the working of break and continue in Python along with some examples.
What Is a Break Statement In Python?
The break statement is a control flow statement in Python which allows an exit loop before it completes based on a given condition. Break statements can be used with for, while as well as nested loops. The control passes to the next available statement outside the loop immediately.
For instance, you want to terminate a loop then you need to declare a condition in which the loop breaks. Let us say we want to iterate over a list of numbers and stop the loop when the number become greater than 5.
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for num in numbers: if num > 5: print(f”Breaking the loop at {num}”) break print(num) |
The above code is the program you need to break out of the loop when the number becomes greater than 5. Check the condition where the number “num” becomes greater than 5, “break” statement comes into action.
Break and Python In Python Key Takeaways
- Break statement exists the loop completely
- Continue statement skips the current iteration in the loop and then proceeds to the next iteration.
- Both break and continue in Python can be used with for and while loops.
What Is a Continue Statement In Python?
The Continue statement in Python is used to skip a part of the code based on the condition given but it stays inside the loop if there are other iterations left. It skips the iteration for the condition which satisfies the current iteration.
Let us interchange the break and continue in Python for our previous example and skip the “5” number instead of skipping the entire loop after that. Let us write a program for the given statement.
Read more: Python Full Stack Developer Roadmap for 2025
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for num in numbers: if num = 5: continue print(num) |
As soon as the number becomes equal to 5 the loop skips the current iteration and then prints the other remaining numbers i,e. 6, 7, 8, 9.
Working of Break Statement In Python
In the break statement inside a for or while loop in Python a condition is given to execute the break statement. Let us understand this flowchart with a simple example.
for i in range(1, 10):
if i == 5: print(“Breaking the loop at i =”, i) break # Exit the loop when i is 5 print(i) |
In this example, we will take Python for a loop in which the range start from 1 to 10 where the break statement condition is 5. Hence, when the loop value iterates at 5 then we have to break out of the loop. In each iteration the loop keeps on checking the iterable and when the condition evaluates to “Yes” it breaks out of the loop and the process ends.
Working of Continue Statement In Python
In the continue statement there is only a slight difference from the break statement as here we will not exit the loop immediately. We only skip the current iteration based on the condition which satisfies.
for i in range(1, 10):
if i == 5: print(“Skipping i =”, i) continue # Skip the rest of the loop when i is 5 print(i) |
The for loop starts its execution and runs and print the numbers starting from 1 to 4. However, the current iteration where “i = 5” it skips printing this number and skips to the next number and start printing the other numbers i,e. 6, 7, 8, 9
Read More: Python Scripting: Automate Your Daily Tasks with Python
Real World Examples with Break and Continue In Python
Let us check some real world example problems which can be solved using the break and continue statement in Python.
1. Write a program to search for an item in a list using break statement.
shopping_cart = [‘apple’, ‘banana’, ‘orange’, ‘grapes’, ‘mango’]
search_item = ‘grapes’ for item in shopping_cart: if item == search_item: print(f”{search_item} found in the cart!”) break else: print(f”{search_item} is not in the cart.”) |
Output
![]() |
2. Write a program to skip the invalid data using the continue statement in Python
Let us take a list consisting of users’ age and we need to skip any negative age in the list using the continue statement without manually opting out from the list.
user_ages = [25, 30, -5, 40, 15, -2, 60]
for age in user_ages: if age < 0: print(f”Invalid age {age}, skipping.”) continue # Skip the invalid ages print(f”Processing age: {age}”) |
Output
![]() |
3. Write a Python program to handle user input using break and continue in Python.
You are building a system that prompts the user for their input and processes the input until a valid response is obtained. If the input is valid, it should ask again if the input is exit then it should break out from the loop.
while True:
user_input = input(“Enter a number between 1 and 10 or type ‘exit’ to quit: “)
if user_input.lower() == ‘exit’: print(“Exiting the system.”) break try: number = int(user_input) if number < 1 or number > 10: print(“Number is out of range. Please try again.”) continue print(f”Valid input received: {number}”) break except ValueError: print(“Invalid input. Please enter a valid number.”) continue |
Output
![]() |
Suppose a user wants to “exit” after giving some wrong input value for the range.
![]() |
Read More: Python Tuple Function: Complete Overview For Beginners
Difference Between Break And Continue In Python
Let us understand some of the basic difference between the break and continue in Python.
Break statement | Continue statement |
Break statement is used to exit the loop entirely. | Continue statement skips the current iteration and continues with the next iteration. |
With a break statement the loop terminates immediately. | The Continue statement is used to skip the rest of the current loop iteration and moves to the next iteration. |
Break statement works with both for and while loops. | Continue statement works with both for and while loops. |
The Break statement exits the loop, and the program continues executing after the loop. | The Continue loop keeps running, but the current iteration is skipped. |
When you want to stop the loop based on a condition. | When you want to skip some part of the loop for a specific condition. |
The loop stops executing and control moves to the statement following the loop. | The loop continues with the next iteration, ignoring the rest of the code for the current iteration. |
for i in range(10):
if i == 5: break |
for i in range(10):
if i == 5: continue |
Become a Python Programmer With DSA Experience
Build your experience in Python programming with PW Skills Decode DSA With Python Course where you will learn every fundamentals of Python programming, practice real world questions, solve module level assignments, work on real projects and much more with in-depth pre-recorded tutorials available on the platform.
Complete all assessments with over 60% marks and download your certificate from PW Skills. Strengthen your knowledge in Data structures, algorithms, and competitive programming with this all in one Python course.
Break and Continue in Python FAQs
Q1. What is a Break statement in Python?
Ans: The break statement is a control flow statement in Python which allows an exit loop before it completes based on a given condition. Break statements can be used with for, while as well as nested loops.
Q2. What is a Continue statement in Python?
Ans: The Continue statement in Python is used to skip a part of the code based on the condition given but it stays inside the loop if there are other iterations left.
Q3. Can we use a for loop with the break statement in Python?
Ans: Yes we can use python for loops with the break statement easily to break out of the loop. We can also use the while loops with break statements in Python.
Q4. Can we use a while loop with the continue statement in Python?
Ans: Yes we can use a while loop with both continue and break statements in Python.