Learn Java
import com.learnjava.controlflow.DoWhileLoop;

do-while Loop in Java

@Concept

A do-while loop is a while loop that checks its condition after the body runs instead of before, which guarantees the body executes at least once no matter what the condition turns out to be. do { body } while (condition); reads almost like a while loop with the parts reordered, but that single difference in when the check happens is the entire reason to reach for one.

The classic use case is a menu or input-validation loop, where the code needs to show the menu — or ask the question — at least once before there's anything meaningful to check the condition against. A plain while loop would need the same logic duplicated once before the loop and once inside it to get that same guaranteed-first-run behavior.

@HowItWorks
1

The syntax

do { body } while (condition); runs the body, then checks the condition afterward.

2

Guaranteed first run

Because the check happens after the body, the body always executes at least once, regardless of the condition's initial value.

3

Subsequent iterations behave like a while loop

After that first guaranteed run, the condition is checked before every further iteration, exactly like an ordinary while loop.

4

The semicolon after while

A do-while loop's closing while (condition) needs a semicolon after it, unlike a plain while loop's header.

5

When it beats a plain while loop

Anywhere the body needs to run once before there's anything to meaningfully check, like showing a menu before asking whether to show it again.

@Example
public class DoWhileDemo {
public static void main(String[] args) {
int attempts = 0;
do {
attempts++;
System.out.println("Attempt " + attempts);
} while (attempts < 3);
}
}
Output
Attempt 1
Attempt 2
Attempt 3
1The body runs first, unconditionally, incrementing attempts to 1 and printing "Attempt 1".
2Only after that does attempts < 3 get checked; since 1 < 3 is true, the loop continues.
3The same pattern repeats for attempts 2 and 3, with the condition still true after the second run.
4After the third run, attempts < 3 becomes 3 < 3, which is false, so the loop stops — three lines of output in total.
@UseCases
Showing a menu at least once before asking whether the user wants to see it again
Validating user input where the prompt itself needs to appear before any check can run
Retrying an operation at least once even if the retry condition would otherwise start out false
Choosing do-while specifically to avoid duplicating setup logic before and inside a plain while loop

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.