Python Break & Continue statements

Tutorial Playlist

In this tutorial, we will learn Python Break and Continue Statement in detail. Python break statement is used to terminate the loop, and the continue statement is used to skip the current iteration of the loop. We will also learn the flow chart of the break and continue statement in Python.
 

Table of Contents

Break Statement

Break Statement in Python

A break statement in Python is used to terminate the loop based on a specific condition defined in the program. Once the break statement is executed, the iteration of the loop stops immediately and exits the loop, then the program continues with the next line of code after the loop.

If you want to learn more about Python loops, refer to this: Python Loop Tutorial

Syntax of Break Statement

Here is the syntax of Break statement in Python:

Code:

for / while loop:

if condition:

 break

Working of Python break Statement

Python

Here is the flow chart that explains the working of the Python break statement more clearly:

Explanation:

  • Step 1: First of all the loop condition is checked, and if the condition is true, a break statement will be executed. And it terminates the loop.
  • Step 2: if the condition is false, it goes to the next statement.

Python Break Statement with For Loop

We can use Python break statements with a for loop to terminate the execution of the loop. Let’s learn this with an example:

Code:

for i in range(10):
if i==5:
break
else:
print(i)

Output:

0

1

2

3

4

Explanation: Here, we have run a loop from 1 to 10, but it will print only 5 elements because, according to the condition given, if i == 5, the break statement will be executed and the loop terminates.

Python Break Statement with While Loop

We can use Python break statements with a while loop to terminate the execution of the loop. Let’s learn this with an example:

Example:

i = 0
while i < 10:
 if i == 5:
   break
 print(i)
 i += 1

Output:

0

1

2

3

4

Explanation: if i == 5: this statement terminates the loop, and 5 values are printed from 0 to 4.

Python: From Basics to Industry Excellence
Hands-On Training for High-Demand Skills. Build, Solve, and Thrive in the Tech World!
quiz-icon

Continue Statement in Python

The ‘continue’ statement is used to skip the execution of the current iteration of a loop and move to the next iteration. It is the opposite of the ‘break’ statement, which forces to stop the execution of the loop immediately. Instead of terminating the loop, the ‘continue’ statement helps us to skip the current iteration of the loop and continue with the next iteration.

Syntax of Continue Statement in Python:

for / while loop:
if condition:
continue

Working of continue Statement in Python

Python

Here is the flow chart that explains the working of the Python continue statement more clearly:

Explanation:

  • First of all, we come into the loop body and check if the condition is true (that contains a continue statement), then we will skip the iteration and go to the next iteration.
  • If the condition is false, then we will continue with the normal loop flow.

Python Continue Statement with For Loop

We can use Python continue statements with a for loop to skip the current iteration of the loop. Let’s learn this with an example:

Example:

for i in range(10):
if i==5:
continue
else:
print(i)

Output:

0

1

2

3

4

6

7

8

9

Explanation: Here, the continue statement will skip the current iteration and proceed with the next iteration, so from 0 to 10, it will skip 5 and print the rest digits.

Python Continue Statement with While Loop

We can use Python continue statements with a while loop to skip the current iteration of the loop. Let’s learn this with an example:

Example:

i=0
while i<10:
if i==5:
i+=1;
continue
else:
print(i)
i= i+1;

Output:

0

1

2

3

4

6

7

8

9

Explanation: Here, the continue statement will skip the current iteration and proceed with the next iteration, so from 0 to 10, it will skip 5 and print the rest digits.

Free Python Course – Your Gateway to Industry-Level Expertise!
Build real-world projects and kickstart your tech career today!
quiz-icon

Real-life examples of Python Break and Switch Statements

1. Print the even numbers from 1 to 10.

We can solve this question using the continue statement in Python:

Code:

for i in range(20):
 if(i%2==0):
  print(i)
 else:
  continue

Output:

0

2

4

6

8

10

12

14

16

18

2. Check whether an element exists in the matrix or not.

We can solve this problem using the break and continue statements in Python.

Code:

matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
target = 5

for i in range(len(matrix)):
for j in range(len(matrix[i])):
if matrix[i][j] == target:
print(“Element Found”)
break
else:
continue
break
else:
print(“Element not found”)

Output:

Element Found

Get 100% Hike!

Master Most in Demand Skills Now!

Conclusion

In conclusion, Python break and continue statements are used to control the loop iteration. Break statements are used to stop the execution of the loop, and continue statements are used to skip the current iteration of the loop and continue with the next iteration. So far in this article, we have learned Python break and continue statements with for loop and while loop. If you want to learn more about Python and explore the career opportunities in this domain, you can enroll in Intellipaat’s Python Course.

 

Our Python Courses Duration and Fees

Program Name
Start Date
Fees
Cohort starts on 26th Jan 2025
₹20,007