Break in Python: A Step by Step Tutorial to Break Statement

‘Break’ in Python is a loop control statement. It is used to control the sequence of the loop. Suppose you want to terminate a loop and skip to the next code after the loop; break will help you do that. A typical scenario of using the Break in Python is when an external condition triggers the loop’s termination. 

Another use case for using ‘Break’ in Python is when you’ve taken an input for something, printed it using a loop, and want to give the user an option to print it again. If the user inputs “No,” you can terminate the loop. Seems a bit confusing? Don’t worry, this tutorial will look into some examples and how it works, in layman’s terms.

You can use break in Python in all the loops: while, for, and nested. If you are using it in nested loops, it will terminate the innermost loop where you have used it, and the control of the program will flow to the outer loop. In other words, it breaks the sequence of the loop, and the control goes to the first statement outside the loop.

Python Training Course

Learn Data Operations in PythonExplore Course
Python Training Course

Syntax of Break in Python

The syntax is as follows:

break;

It is used after the loop statements.

Flowchart of Break in Python

The following flowchart shows the use and control flow of a break statement in a loop.

loop-enters

Using Break in Python

Since it is now clear what a break statement is, it’s time to look at some examples and understand how to use them. In each instance, you will be using Break in Python with different loops.

Using Break in While Loop

using-break

As you can see in the example above, there is a defined integer n with the value 0. Then, there is a defined while loop for printing the value of n and increasing it by one after each iteration. Next, you saw how it defined a condition for the break statement, which is when n becomes 5. When the condition is met, the break statement terminates the loop, and the control goes to the next statement, which is the print statement.

Data Science with Python

The Ultimate Ticket To Top Data Science Job RolesExplore Course
Data Science with Python

Using Break in For Loop

using-break-loop

The example above has used the break in Python in the for loop. The for loop iterates through each letter of the word “Python.” When the iteration comes to the letter “o,” and the break condition is met, it terminates the loop.

You can also define two break conditions in a single loop. Let’s look at an example where you can define two conditions while using Break in Python.

using-break-loop

As you can see, the loop was terminated when one of the conditions was met.

Using Break in Nested Loops

nested-loops

In the above example, you saw two lists with four integer values in each. It then used two for loops to iterate through the lists and multiply the integers. Next, it defined the break conditions. When the break condition was met, the innermost loop was terminated, and the control went back to the outer loop.

Looking forward to making a move to the programming field? Take up the Python Training Course and begin your career as a professional Python programmer.

Conclusion

Python has numerous use cases and is turning out to be one of the most popular programming languages worldwide. Break in Python is just a basic concept. But if you want to pursue a career in the software development field, you can opt for our Python Certification Course. The course provides you with a vast learning resource to help you excel in the Python programming language.

Have any questions for us regarding ‘Break in Python’? Tell us in the comments section, and our experts will get back to you on it, right away!

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.