Headings of Contents
Python Break Statement
In this tutorial, we are going to learn about the Python Break statement and how can we use Python break statement in Python to terminate the flow of control.
In the previous tutorial, we have seen Python for a loop as well as Python while loop which is used to execute a block of code for specific numbers of times. In this guide, we will see how to use the break statement to terminate the loop even condition return True.
What is Break Statement in Python ?
In Python, Break is a keyword that is used to terminate the loop even the condition returns True. Break statement maximum used with Loop like if.else, Python for loop, Python while loop, etc.
Why the use of Break statement in Python?
The break statement is used in python when you want to change the flow of control.
In python or other programming, language loops are iterate over a block of code until a test expression is false but sometimes we want to change the current iteration of the program, Then we have the best keyword break, That is used to change the flow of control.
Syntax of Break statement in Python.
break
Example of Python Break Statement:
Example 1
i = 1
while i < 10:
print(i)
if i == 5:
break
i = i + 1
Output
1
2
3
4
5
Example 2
s = 'python'
for i in s:
print(i)
if i == 'o':
break
Output
p
y
t
h
o
Conclusion:
In this tutorial, you have learned what is Python break statement and how to use the Python break statement to terminate or change the flow of control. In Python, the break is very useful because sometimes we want to terminate our program even if the condition returns True.
I hope this tutorial will help you. if you like this tutorial, Please comment and share with your friends who want to learn Computer Programming.