Python
loop
do-while
programming
control-structure

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.

Browse interview questions

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:

python
1while True:
2    value = input("Enter a positive number: ")
3    number = int(value)
4
5    if number > 0:
6        break
7
8print("Accepted", number)

That behaves like a do ... until number > 0 loop:

  • the body always runs once
  • the condition is evaluated after the body
  • 'break exits 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 condition continues while the condition is true'
  • 'do ... until condition continues until the condition is true'

In Python, both patterns use while True, but the break test changes.

For a do ... while style loop:

python
1while True:
2    line = input("Continue? ")
3    if line != "y":
4        break

For a do ... until style loop:

python
1while True:
2    line = input("Type exit to stop: ")
3    if line == "exit":
4        break

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.

python
1def read_command():
2    while True:
3        command = input("command: ").strip()
4        if command:
5            return command
6
7cmd = read_command()
8print(cmd)

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 ... until syntax.
  • The standard equivalent is while True: followed by a break when the stop condition is satisfied.
  • This pattern preserves the "run at least once" behavior.
  • Be careful not to confuse do ... while with do ... until because their conditions are inverted.
  • For complex loops, a helper function can make the control flow clearer than an inline infinite loop.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.