Python control flow – while loop and break statement – with example

  • Reading time:16 mins read

Python control flow – The while loop and the break statement

The while loop in Python and other programming languages enables us to execute a group statement multiple times as long as a given condition remains True.  The break statement on the other hand is a control statement that is often used with loops.

 

 

The break statement allows us to opt-out of a loop when a given condition is satisfied. The break statement is also used to terminate a certain switch statement in Python. In its simplest form, we can use the while statement to print statements multiple times as long as a given condition remains True.

 

python control flow

Python control flow – Basics

In the example below we have initialized two variables namely a and b.  We are going to use the while loop to create a logical statement saying that as long as variable b is less than variable a, print the following statement. In the process, we are going to increment the value of variable b gradually by one. 

Therefore this means that at some point the value of variable b will be equal to the value of variable a. It is at this point the while loop will stop executing since the logical statement b < a will no longer be True.

 

 

Python control flow – Break statement

For some reason, you may want to break out of the while loop at a certain point, in this scenario the break statement can be very useful. For example, if we want the while loop in the example above to break when the value of variable b gets to 8, we can do so using the break statement.

 

python control flow

 

To get started we will first write an if statement inside the while loop. This if statement is going to check if the value of b has increased to 8, and then break out of the loop.

Before breaking out of the loop we can also add a print statement saying that the condition has been met and we can now exit out of the loop.

 

 

Unlike in the earlier example where the while loop executed the print statement until the value of b got to 10, in this case, we have stopped it when the value of b is at 8

The break statement can also be useful when trying to stop an infinite while. Such a loop can potentially exhaust your memory unless there is external intervention. 

 

python control flow

 

Python control flow – Break statement with for loop

A for loop is normally used to iterate over a sequence such as a list or a tuple.  We can also use the break statement to break out of a for loop when iterating over a certain sequence. In the example below we are going to create a simple list and use a for loop to iterate over the list.

While iterating through the list we are going to print each of the items in the list until we get to the floating-point number in the list. Now when we get to the floating-point number we are going to execute the break statement and opt-out of the loop.

 

 

Summary

So this is how you can handle Python control flow in a nutshell. If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.

In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Programming tutorials

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING

Not sure if programming is for you? With CodeBerry you’ll like it.