Is there a do ... until in Python?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Python does not have a native do ... until or do ... while loop syntax. The usual Python pattern is to write a while True loop, run the body once, and break when the stopping condition has been reached.
What do ... until means
A do ... until loop has two defining properties:
- the body runs at least once
- the exit condition is checked after the body
That differs from a normal while loop, which checks the condition before the first iteration and may run zero times.
The standard Python translation
The most common Python equivalent is this pattern:
That behaves like a do ... until number > 0 loop:
- the body always runs once
- the condition is evaluated after the body
- '
breakexits when the condition becomes true'
This is the idiomatic answer in Python.
do ... while versus do ... until
Many developers casually mix these up, but the logic is inverted:
- '
do ... while conditioncontinues while the condition is true' - '
do ... until conditioncontinues until the condition is true'
In Python, both patterns use while True, but the break test changes.
For a do ... while style loop:
For a do ... until style loop:
That distinction matters when you translate code from another language.
Another clean option: wrap the action in a function
If the loop body is complex, moving the work into a function often makes the intent clearer.
This is still a post-check loop pattern, but the control flow becomes easier to read and test.
Why Python does not add dedicated syntax
Python generally prefers a smaller set of control structures and expects programmers to compose them cleanly. Because while True plus break already expresses post-check looping, a separate do keyword has never been necessary in the language.
That can feel unfamiliar if you come from C, Java, or PHP, but in Python codebases the while True pattern is normal and readable.
Common Pitfalls
The biggest mistake is trying to translate a do ... until loop into while not condition: without thinking about first-iteration behavior. That changes semantics because the body may not run at all.
Another issue is writing a while True loop but placing the break condition before the main work. If you check too early, you have accidentally turned it back into a pre-check loop.
It is also easy to overuse while True for logic that would be clearer as a function returning early, especially when several nested conditions are involved.
Finally, when translating from another language, make sure you know whether the source loop is do ... while or do ... until. Reversing the condition is a common bug.
Summary
- Python has no native
do ... untilsyntax. - The standard equivalent is
while True:followed by abreakwhen the stop condition is satisfied. - This pattern preserves the "run at least once" behavior.
- Be careful not to confuse
do ... whilewithdo ... untilbecause their conditions are inverted. - For complex loops, a helper function can make the control flow clearer than an inline infinite loop.
Related reading
- Is there a 'foreach' function in Python 3?
- Is there a function in Python which generates all the strings of length n over a given alphabet?
- Is there a label/goto in Python?
- Is there a library function for Root mean square error RMSE in python?
- Is there a list of Pytz Timezones?
- Is there a list of Pytz Timezones?
- Is there a math nCr function in Python?
- Is there a not equal operator in Python?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.