What is While-True-Break in Python?
When you should create an infinite loop
Iteration structures—code blocks that repeat—are an absolute fundamental tool. Previous articles have discussed iteration structures at a more basic level, but this article will introduce a specific implementation.
We will introduce and explore an intentionally designed infinite loop, something that is normally considered a big no-no.
What is an Infinite Loop?
Before we get into our exact implementation, let’s introduce the concept of an infinite loop. Normally, a loop is intended to eventually exit via some condition. An infinite loop is simply a loop that will not naturally end on its own.
Let’s say we’re building a loop to count down to New Years. We start the countdown at 10 and set up our “Happy New Year!” message, but we forget to modify our counter variable.countdown = 10while countdown > 0:
print(countdown)print('Happy New Year!')
The above code will run forever because we missed a critical statement to modify countdown. The fixed code will look like this:countdown = 10while countdown > 0:
print(countdown)
countdown -= 1print('Happy New Year!')
So, when is an infinite loop not a mistake? Let’s put a hold on that question and introduce one other concept before we get to the good stuff.
What is a Break Statement?
The ‘break’ keyword is a command that will exit the current iteration structure. It’s essentially an exit ramp on the highway.
Let’s take our previous example and add in a break statement to exit at 5.countdown = 10while countdown > 0:
print(countdown)
countdown -= 1if countdown == 5:
breakprint('Happy New Year!')
Our output would look like this.10
9
8
7
6
Happy New Year
Now that we know about infinite loops and break statements we’re ready for the while-true-break strategy.
What is While-True-Break?
Simply put, while-true-break is a coding convention that intentionally uses an infinite loop broken by a break statement.
Let’s say we want to ask the user for a number between 1 and 10.while True:
guess = int(input("Pick a number between 1 and 10: "))
if guess >= 1 and guess <= 10:
break
The while-true-break is not without controversy. Many will argue that it is poor design to use either an infinite loop or a break statement to circumvent the intended exit criteria of a while loop… imagine how they feel about both in one solution!
In fairness, I agree it is a roundabout solution. However there are two reasons why I use this in Python.
The first is that I choose to prioritize readability over standard convention. I find it easier to both read and maintain a while-true-break instead of potentially two input() statements, one outside the while loop and one inside or a flag variable to control the execution of the loop.
Secondly, Python does not have a do-while loop which is the control structure I use while-true-break as substitute for. A do -while loop executes the loop’s body first and checks the expression second.
Conclusion
What do you think? Is while-true-break something you use or would use in your own code? Or do you scowl at the thought of it?
I hope you liked this article! Subscribe to be notified when I publish new articles or even better, support me by joining the Medium community as a paid subscriber.