All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Break A Loop In Python

Last Updated : Mar 11, 2024

How To Break A Loop In Python

In this article we will show you the solution of how to break a loop in python, in Python, we can use break statement to execute a loop in python. If a condition is successfully satisfied then break statement is exit the loop.

We use for loop and while loop to breaking a loop in python. By using break statement, we want to identify the breakpoint in the loop where we want to breaking the execution.

When the condition is satisfied successfully, we can run the break statement to hault the loop.

It note that the break statement only lies the innermost loop. If you have loop within loop i.e nested loop, the break will breaking out of the nested loop.

If we want to breaking out multiple innermost loops, so we can use control structures including return statement or flags.

Break statement allowing to add flexibility in the loop.

It easily allows a way to terminate a loop based on a given condition, It also improving the execution of your code and avoid redundant iterations.

Step By Step Guide On How To Break A Loop In Python :-

Method 1 - Using for loop to break a loop in python

num= [1,3,5,4]
for i in num:
     if i==4:
         break
    print(i)
  1. Firstly, we can take an input array [1,3,5,4] called num
  2. Secondly, we iterate over each element in num using for loop
  3. Then, check the if the current element “i” is equal to value of num i.e 4 and exit the loop when the condition is satisfied using break statement.
  4. If the condition is not met then print the value of i.
  5. Hence the output of this code: 1, 3 and 5

Method 2 - Using while loop to break a loop in python

c=0
while c<3:
        if c==2:
              break
        print(c)
        c+=1
  1. Firstly, we can initialize a count variable “c” to 0.
  2. Secondly we can use while loop it continues indefinite times when the condition is not satisfied. In this code, we can check the value of count is less than 3.
  3. Within the loop, we can check if c is equal to 2 then break statement terminate the loop immediately
  4. c+= 1 statement defines that after print the value of c, then increase the value of c by 1.
  5. Hence, the output of this code:- 0 and 1

Conclusion :-

In conclusion, we can easily say that breaking a loop in python provides simplicity.

We use for loop and while loop to breaking a loop in python. In for loop, we can iterate each elements and define a if condition within a loop.

If the condition is successfully met then break statement will terminating the loop. In while loop, we can define condition within a loop.

If the condition is successfully met then break statement will terminating the loop.

I hope this article on how to break a loop in python helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪