Do-While Loop in Python (With Examples)

Do-while loops don’t exist in Python, but a while True loop that checks the condition at the bottom and exits with break does the same job. The body always runs at least once, just like a do-while in C, Java or JavaScript.

while True:
    # body: always runs at least once
    ...
    if not condition:
        break

The screenshots come from Python 3.12.5 in a real Command Prompt, typed answers and all.

You’ll see why this pattern is the standard one, a menu that asks at least once, how to translate a C-style do-while, and two alternatives that also work.

Does Python have a do-while loop?

No. Python’s only loops are for and while, and both check their condition before running the body. A do-while checks afterward, which guarantees one run.

Python’s developers did consider adding one. PEP 315 proposed a do-while style loop, and it was rejected because nothing beat the existing while True plus break pattern.

So that pattern isn’t a hack. It’s the officially accepted way to write a do-while loop in Python.

Do-while loop vs while loop in Python

The difference only shows when the condition is false from the start. A regular while loop skips the body entirely; the do-while style runs it once and then stops:

count = 10

print("while loop:")
while count < 5:                 # false from the start
    print("  ran with", count)
    count += 1
print("  (the body never ran)")

count = 10
print("do-while style:")
while True:
    print("  ran with", count)   # runs once before any check
    count += 1
    if not count < 5:            # the condition, checked at the bottom
        break

Output:

while loop:
  (the body never ran)
do-while style:
  ran with 10
Command Prompt output showing a Python while loop that never runs next to a do-while style while True loop that runs once
Same starting value, same condition. Only the do-while version ran its body.

Notice that the condition is negated at the bottom: the loop continues while count < 5, so it breaks when that’s not true. If you need a refresher on the regular version, see Python while loop.

A do-while loop for user input in Python

This is the classic reason to want a do-while: you have to ask once before you know whether to ask again. Here a shopper keeps adding items until they answer anything but y:

cart = []

while True:
    item = input("Item to add: ")
    price = float(input("Price in USD: "))
    cart.append((item, price))

    again = input("Add another? (y/n): ")
    if again.strip().lower() != "y":
        break

total = sum(price for _, price in cart)
print(f"{len(cart)} items, total ${total:,.2f}")

Output:

Item to add: Coffee mug
Price in USD: 12.99
Add another? (y/n): y
Item to add: Notebook
Price in USD: 4.50
Add another? (y/n): n
2 items, total $17.49
Command Prompt showing a Python do-while style loop asking for an item and a price in US dollars until the user answers n
Two items added, then n ended the loop. The question always gets asked at least once.

With a plain while, you’d have to ask the first question before the loop and repeat it inside. The while True shape keeps every question in one place.

I’d add a try/except ValueError around the price in real code, since someone will eventually type “twelve”.

How to convert a C-style do-while loop to Python

If you’re translating code from C, Java or JavaScript, the steps are mechanical: the body goes inside while True, and the while (...) condition becomes a negated if with break:

# the C / Java / JavaScript version:
#   int i = 1;
#   do {
#       printf("%d\n", i);
#       i++;
#   } while (i <= 3);

i = 1
while True:
    print("i =", i)
    i += 1
    if not (i <= 3):             # the do-while condition, negated
        break

Output:

i = 1
i = 2
i = 3
Command Prompt output of a Python while True loop translated from a C do-while loop printing i from 1 to 3
The same output as the C version in the comment: 1, 2 and 3.
C / Java / JavaScriptPython
do {while True:
body; body
} while (condition); if not condition: break

Watch out for continue inside the translated loop: in Python it jumps back to the top and skips your if ...: break check. Python break and continue explains that trap.

Other ways to write a do-while loop in Python

Two other shapes also guarantee one run. The first uses a variable that starts as True and gets updated at the end of the body:

balance = 40.00
withdrawals = [25.00, 30.00, 10.00]
keep_going = True                # starts True, so the body always runs once

while keep_going:
    amount = withdrawals.pop(0)
    if amount <= balance:
        balance -= amount
        print(f"withdrew ${amount:.2f}, balance ${balance:.2f}")
    else:
        print(f"declined ${amount:.2f}, balance ${balance:.2f}")
    keep_going = bool(withdrawals) and balance > 0

Output:

withdrew $25.00, balance $15.00
declined $30.00, balance $15.00
withdrew $10.00, balance $5.00

That reads well when the “keep going” decision depends on several things, like here, where the loop stops when there are no withdrawals left or the balance hits zero.

The second uses the walrus operator, :=, from Python 3.8. It fits loops where the body is mostly “get a value, check it”:

import random

random.seed(2026)

# roll until a six, at least once; := keeps it to one loop line
rolls = 0
while (roll := random.randint(1, 6)) != 6:
    rolls += 1
    print("rolled", roll)
print("got a six after", rolls + 1, "rolls")

Output:

rolled 1
rolled 3
rolled 5
rolled 5
got a six after 5 rolls

The seed makes the rolls repeatable, so you’ll get the same result if you run it. Of the three styles, while True with break is the one most Python developers will recognize instantly, so it’s my default.

Python do-while loop patterns compared

PatternRuns at least once?Best for
while True: … if not cond: breakYesMost cases; the standard idiom
keep_going = True / while keep_going:YesA stop decision built from several checks
while (x := get()) != stop:Yes, the call always runsGet-a-value-then-test loops
while cond:NoNormal loops that may not need to run

To get more comfortable with loops in Python, read these next:

Frequently asked questions

Does Python have a do-while loop?

No. Use while True: with an if not condition: break at the end of the body. It always runs at least once.

How do you write a do-while loop in Python?

Put the body inside while True: and finish it with if not condition: break, where condition is the one you’d put after while in C or Java.

What is the difference between a while loop and a do-while loop?

A while loop checks its condition before the first run, so it may run zero times. A do-while checks afterward, so it always runs at least once.

Why doesn’t Python have do-while?

It was proposed in PEP 315 and rejected, because no new syntax was clearer than while True with break.

Can I use continue in a Python do-while loop?

Yes, but it jumps back to the top and skips the break check at the bottom. Make sure whatever continue skips can’t leave the loop running forever.