---
title: The do-while Equivalent in Python (and Nested Loops)
source: https://app.sythra.ai/learn/python/do-while-python
topic: Python
updated: 2026-08-12
publisher: Sythra (https://app.sythra.ai)
---

# The do-while Equivalent in Python (and Nested Loops)

Python has no built-in do-while loop, but while True: combined with a break inside the body simulates the same run-at-least-once behavior — commonly used for input validation.

_Source: [https://app.sythra.ai/learn/python/do-while-python](https://app.sythra.ai/learn/python/do-while-python) — free to read on Sythra._

## Key points

- Python has no do-while keyword; simulate it with while True: and a break
- The body always runs at least once before the exit condition is checked
- This pattern is standard for validating user input that must be asked at least once
- Loops can be nested inside other loops; the inner loop fully completes for every pass of the outer loop
- Nested loops multiply total iterations — a 10x10 nest runs the body 100 times, not 20

Some programming languages have a special loop called `do-while`, which is just like [while](/learn/python/while-loop-python), except it checks its condition **after** running the body instead of before — guaranteeing the body runs **at least once**, even if the condition would have been false from the very start.

> **Python has no do-while keyword:** 

## What you will learn

- How to simulate a run-at-least-once loop with `while True:` and `break`
- A real pattern: validating user input that must be asked at least once
- How to nest loops inside other loops
- Why nested loops multiply your program's total work

## Simulating do-while with while True

The trick is to start with a condition that's guaranteed to be true, and then decide for real whether to continue from inside the body:

```python
while True:
    response = input("Type 'yes' to continue, anything else to stop: ")
    print("You typed:", response)
    if response != "yes":
        break
```

Walk through what this actually does:

- `while True:` guarantees we enter the loop body at least once, no matter what — there's nothing to check beforehand.
- The body runs fully — it asks for input and prints it.
- Only _after_ the body has run does it check whether `response` was `"yes"`. If it wasn't, `break` fires and we exit.

This is the standard, accepted way to simulate a `do-while` loop in Python. You'll see this exact `while True:` ... `break` pattern constantly in real Python code, anytime the rule is "do this thing first, then decide whether to keep doing it."

## A classic use: validating input

Here's another classic use — validating user input, where you absolutely need to ask **at least once**, no matter what:

```python
while True:
    age = int(input("Enter your age: "))
    if age >= 0:
        break
    print("Age can't be negative. Try again.")

print("Thanks! Your age is", age)
```

The program always asks at least one time. It only keeps looping back if the answer given was actually invalid.

## Nested loops: loops inside loops

Just like you can nest [if statements](/learn/python/conditionals-in-python) inside other if statements, you can also place a loop **inside** another loop. This is called **nesting**, and the inner loop will run completely, start to finish, for every single pass of the outer loop.

```python
for i in range(1, 4):
    for j in range(1, 4):
        print(i, j)

# Output:
# 1 1
# 1 2
# 1 3
# 2 1
# 2 2
# 2 3
# 3 1
# 3 2
# 3 3
```

Here's exactly what's happening, step by step: the **outer loop** picks `i = 1` and holds onto it. While `i` is still `1`, the **inner loop** runs completely on its own, cycling through `j = 1, 2, 3` one at a time. Only once the inner loop has fully finished does control return to the outer loop, which then moves on to `i = 2` — and the entire inner loop runs all over again, from scratch, with the new value of `i`.

A very natural real-world use of this is printing a grid or a multiplication table:

```python
for i in range(1, 4):
    for j in range(1, 4):
        print(i * j, end="  ")
    print()    # move to a new line after each row

# Output:
# 1  2  3
# 2  4  6
# 3  6  9
```

That little `print()` with nothing inside it, placed in the _outer_ loop but outside the _inner_ one, is doing important work — it forces a new line to start once each full row of the inner loop is finished, so the numbers actually line up into a proper grid instead of running together on a single endless line.

> **A word of caution about nested loops:** 

You can nest `for` loops inside `while` loops, `while` loops inside `for` loops, or any mixture you like — Python doesn't care what kind of loop is inside what kind of loop. What matters is simply: indentation level tells Python which loop a line of code belongs to.

## Common mistakes

- Trying to use a `do-while` keyword — Python simply doesn't have one
- Forgetting the `break` inside a `while True:` simulation, creating an accidental infinite loop
- Underestimating the total number of iterations in nested loops (it's multiplicative, not additive)
- Losing track of which loop a `break` or `continue` applies to — it always affects only the innermost loop it's written in

> **Practice with Sythra:**  [Practice with AI tutor](https://app.sythra.ai/pricing)

## FAQ

### Does Python have a do-while loop?

No. Python has no do-while keyword. You simulate the same run-at-least-once behavior using while True: with a break statement inside the body, once your exit condition is met.

### How do you validate user input so it's asked at least once?

Wrap the input call in while True:, check the condition after receiving input, and break once the input is valid. This guarantees the prompt runs at least once, unlike a regular while loop that checks its condition before running.

### How do nested loops work in Python?

A nested loop is a loop written inside another loop's body. For every single pass of the outer loop, the entire inner loop runs from start to finish before the outer loop advances to its next value.

### How many times does a nested loop run in total?

Multiply the iteration counts. If the outer loop runs 10 times and the inner loop runs 10 times per outer pass, the innermost code runs 10 × 10 = 100 times total.

## Related

- [The while Loop in Python](https://app.sythra.ai/learn/python/while-loop-python) — The loop that do-while's simulation is built on top of.
- [The for Loop in Python](https://app.sythra.ai/learn/python/for-loop-python) — Looping over a known sequence with for.
- [More Tools for Iteration in Python](https://app.sythra.ai/learn/python/iteration-tools-python) — continue, enumerate(), zip(), comprehensions, and generators.
- [Python course hub](https://app.sythra.ai/learn/python) — All free Python explainers and the path into Agentic practice.

---

Written by Sythra — Learn machine learning by building. Practice this topic with Sythra's AI tutor: https://app.sythra.ai/pricing
