‘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.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

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.

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.

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 Caltech Coding Bootcamp. 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!

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 16 Dec, 2024

6 Months$ 8,000
Automation Test Engineer Masters Program

Cohort Starts: 27 Nov, 2024

8 months$ 1,499
Full Stack Java Developer Masters Program

Cohort Starts: 18 Dec, 2024

7 months$ 1,449
Full Stack (MERN Stack) Developer Masters Program

Cohort Starts: 8 Jan, 2025

6 Months$ 1,449