AlgoMaster Logo

Do-While Loop

Medium Priority10 min readUpdated June 2, 2026
Listen to this chapter
Unlock Audio

A do-while loop runs its body first, then checks the condition. That tiny reordering matters a lot in practice. When a block of code must always execute at least once before deciding whether to repeat, do-while is the right loop.

The Basic Shape

A do-while loop has three parts: the do keyword, a body block in braces, and a while (condition); at the bottom. That trailing semicolon is part of the syntax, not optional.

The flow goes: enter the body, run it, evaluate the condition, loop back to the body if it's true, exit if it's false. The condition lives at the bottom, so the body has already executed once before the loop even thinks about whether to continue.

How Execution Flows

The control flow is short to describe but useful to picture. A do-while body always runs, then the condition decides whether to run it again.

There is no arrow that lets the program skip the body. Even if the condition would have been false from the start, the body still runs once. That's the reason do-while exists.

Contrast With While

A regular while loop checks the condition first. If it's already false, the body never runs at all. A do-while runs the body, then checks. Same code, different starting condition, different output.

Here's the same logic written both ways. The starting balance is already below the threshold:

The while loop printed nothing from its body because the condition was already false on entry. The do-while printed once, then checked the condition, found it false, and stopped. That single guaranteed pass is the defining feature.

The Mandatory Trailing Semicolon

The semicolon after while (...) is required. Without it, the compiler refuses to build. This is the most common mistake learners make with do-while.

What's wrong with this code?

This won't compile. The compiler produces an error like:

The fix is one character. Put the semicolon back at the end of the while (...) line:

Fix:

Why does Java demand it? The do-while statement is a single statement and Java statements terminate with a semicolon. The while part of a normal while loop doesn't need one because it's followed directly by the body block, but here the body comes first and the condition closes the whole statement.

Input Validation

The best use case for do-while is reading input and checking if it's valid. Reading has to happen first, otherwise there's nothing to check. That fits do-while perfectly: act first, then validate.

To keep the output predictable, this example simulates input with a small array instead of reading from the keyboard. The logic is the same. Each attempt is an entry from attemptedIds. The loop keeps going until it sees a valid product ID.

The body runs, reads an attempt, then the condition decides whether the value was good. Bad input loops back. Good input falls out the bottom. Writing this with a regular while is awkward because it would require either priming the variable with a dummy value or copying the read step both before and inside the loop.

Here's the same idea with Scanner when actual keyboard input is needed. The expected output below assumes the user typed -2, then 0, then 42:

Retry Until Success

The same pattern fits retries. Try something, check the result, try again if it failed. The action happens at least once, so do-while is the right shape. This example simulates checkout attempts where the first two fail and the third succeeds.

The condition has two parts joined with &&: keep going if payment isn't confirmed AND attempts remain. The second part prevents the loop from running forever when every attempt fails.

Menu Loops

Menu loops are another natural fit. Show the menu, take a choice, act on it, then loop unless the choice was "exit". Like input validation, the menu has to appear once before the decision to show it again can be made.

The menu always appears once, the user always picks once, and the loop only exits when the chosen action is exit. A regular while would require printing the menu before the loop and again at the bottom of the body, which duplicates code.

The "Already Valid" Trap

The same property that makes do-while good for validation can backfire. If the input is already correct, the body still runs once. That's only a problem when the body does something that shouldn't repeat unconditionally, like printing a prompt or charging a card.

What's wrong with this code?

The original quantity was already 2, which is positive. The user should never have seen the "Please enter" prompt. The body ran once because that's what do-while does. The fix is to check first when the value might already be valid:

Fix:

Rule of thumb: use do-while when the body has to run at least once to produce the value being checked. Use while when the value already exists and the action should only happen if a condition is true.

Quiz

Do-While Loop Quiz

10 quizzes