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.
Some programming languages have a special loop called do-while, which is just like while, 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.
What you will learn
- How to simulate a run-at-least-once loop with
while True:andbreak - 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:
while True:
response = input("Type 'yes' to continue, anything else to stop: ")
print("You typed:", response)
if response != "yes":
breakWalk 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
responsewas"yes". If it wasn't,breakfires 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:
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 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.
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 3Here'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:
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 9That 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.
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-whilekeyword — Python simply doesn't have one - Forgetting the
breakinside awhile 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
breakorcontinueapplies to — it always affects only the innermost loop it's written in
Common questions
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.
Explore
Related topics
Keep going — these sit next to this concept in a real learning path.
The while Loop in Python
The loop that do-while's simulation is built on top of.
The for Loop in Python
Looping over a known sequence with for.
More Tools for Iteration in Python
continue, enumerate(), zip(), comprehensions, and generators.
Python course hub
All free Python explainers and the path into Agentic practice.