How to emulate a do-while loop?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
A do-while loop runs the body once before checking the condition. If your language does not have that construct, the usual replacement is not a complicated trick; it is a loop that runs unconditionally once and exits with break when the post-condition fails.
What Behavior You Need to Preserve
The defining property of a do-while loop is “execute at least once.” That makes it useful for input validation, menu prompts, retry logic, and any workflow where the condition depends on work done inside the loop body.
A traditional do-while looks like this:
The condition is checked at the end, not at the beginning.
The Best General Pattern: while True Plus Break
In languages such as Python, the clearest emulation is an infinite loop with a terminating break after the work is done.
That structure preserves the two things a do-while promises:
- the body runs at least once
- the continuation condition is evaluated after the body
This is usually clearer than introducing extra flag variables just to fake first-iteration behavior.
A Real Example: Input Validation
A classic use case is asking for input until it is valid.
The prompt must appear at least once, so a pre-condition loop would be awkward. The while True pattern matches the intent directly.
The Flag Variable Alternative
You can also emulate do-while with a flag that forces the first iteration.
This works, but it is usually harder to read because the loop condition mixes actual business logic with scaffolding introduced only to mimic syntax from another language.
Use this pattern only when the exit condition is naturally expressed in the while header and you believe that improves readability.
When a Helper Function Is Better
Sometimes the desire for do-while is really a sign that the work should be extracted into a function that returns whether to continue.
This is useful when the body is long and you want the continuation decision made in one place rather than scattered through the loop.
Language-Specific Notes
Different languages make this easier or harder.
- C, Java, JavaScript, and many similar languages already have a native
do-while - Python does not, so
while Trueplusbreakis the standard idiom - functional languages may prefer recursion or iterator pipelines instead of explicit loop emulation
So the “best” emulation is partly language culture. In Python, a direct while True loop is normal and readable.
Common Pitfalls
The most common mistake is using a flag variable when while True would be clearer. The flag makes readers work harder for no real benefit.
Another mistake is placing the break condition before the main work, which accidentally turns the loop back into a pre-condition loop and loses the guaranteed first execution.
A third mistake is forgetting that multiple break points can make the loop harder to follow. If the loop body grows large, extract logic into functions.
Summary
- A
do-whileloop guarantees one execution before checking the condition. - In languages without
do-while,while Trueplus a post-bodybreakis usually the clearest replacement. - Flag-based emulation works, but it often hurts readability.
- Input validation and retry logic are common cases where post-condition loops fit naturally.
- If the loop body becomes complex, move the work into a helper function instead of layering more loop tricks on top.
.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.