skip to content

What is the difference between a while loop and a do-while loop in Java?

level: juniorimportance: must knowfreq 75%

answer

  1. while = test first = maybe zero times
  2. do-while = body first = at least once
  3. do-while ends with a semicolon
  4. Condition must be boolean
  5. do-while for prompt/menu until valid input

basics

~20 s

A while loop checks the condition before running its body, so it may run zero times. A do-while loop runs the body once first, then checks the condition, so it always runs at least once.

solid answer

~40 s

Both loops repeat a block of code while a boolean condition stays true. The key difference is when the condition is checked. A while loop is pre-test: it evaluates the condition first, and if it is false from the start, the body never executes (zero or more iterations). A do-while loop is post-test: it runs the body once, then evaluates the condition at the bottom, so the body always executes at least once (one or more iterations). Syntactically, do-while ends with a semicolon after the while clause. Use while when the work might not be needed at all (e.g. reading items that may be absent); use do-while when you must do something once before you can decide whether to repeat, such as prompting a user for input and re-prompting until it is valid.

code

java · 11 lines
java
int x = 10;

// while: condition false up front -> body never runs
while (x < 5) {
    System.out.println("while body");
}

// do-while: body runs once before the same check
do {
    System.out.println("do-while body"); // prints once
} while (x < 5);

go deeper

for a junior

States the core rule: while tests first (maybe zero runs), do-while runs once then tests (at least one run).

for a middle

Picks the right loop for a scenario (e.g. do-while for input validation) and remembers the do-while semicolon.

for a senior

Explains why do-while is rare, gives idiomatic use cases, and notes both compile to equivalent bytecode patterns differing only in test placement.

for a principal

Frames loop choice in terms of readability and intent-signaling, and can discuss when to prefer a for-loop or stream/iterator abstraction over either.

## What a loop is A **loop** is a control-flow construct that repeats a block of code (the **loop body**) as long as a **condition** is true. The condition is any expression that evaluates to a `boolean` (`true` or `false`). Each single pass through the body is called an **iteration**. ## The two forms Java has two condition-driven loops that differ only in *when* the condition is tested. ### `while` — pre-test (test at the top) ```java while (condition) { // body } ``` Mechanics: (1) evaluate `condition`; (2) if `true`, run the body and go back to step 1; (3) if `false`, skip the body and continue after the loop. Because the test happens **before** the first iteration, if the condition is false at the very start, the body runs **zero times**. ### `do-while` — post-test (test at the bottom) ```java do { // body } while (condition); // note the semicolon ``` Mechanics: (1) run the body; (2) evaluate `condition`; (3) if `true`, go back to step 1; (4) if `false`, continue after the loop. Because the test happens **after** the body, the body runs **at least once**, even if the condition is false the first time it is checked. ## The single difference that matters - `while` = **zero or more** iterations (might never run). - `do-while` = **one or more** iterations (always runs once). Everything else (how the boolean is evaluated, `break`/`continue` behavior) is identical. ## Worked example ```java int n = 5; while (n < 5) { System.out.println("while"); } // prints nothing — 5 < 5 is false int m = 5; do { System.out.println("do-while"); } while (m < 5); // prints once — body ran before the test ``` ## When to choose which - Use **`while`** when the body might legitimately not need to run at all — e.g. draining a queue that may already be empty, or reading lines that may not exist. - Use **`do-while`** when you must perform an action once *before* you can evaluate whether to repeat — e.g. show a menu and re-show it until the user picks a valid option, or generate a value and regenerate until it passes a check. In both cases the first execution is unconditional. In practice `while` is far more common; `do-while` is rare and used deliberately for the at-least-once guarantee.

  • Can a while loop ever run zero times? What about a do-while?
    Yes — a while loop runs zero times if its condition is false on the first check. A do-while always runs at least once because the body executes before the first condition check.
  • Why does do-while need a semicolon after the while clause but while does not?
    In do-while the while(...) is a terminating statement at the end of the construct, so it requires a semicolon. In a plain while loop, while(...) is immediately followed by the body block, which is itself the statement, so no trailing semicolon is needed.

A while loop is like checking the weather before going out — if it's raining you never leave. A do-while loop is like stepping outside first, then deciding whether to go back in.

saying these in an interview costs you the question

  • Saying both loops always run at least once — only do-while guarantees that.
  • Forgetting the semicolon after the while clause of a do-while (compile error).
  • Claiming the difference is performance — it is purely about when the condition is checked.
  • Thinking the condition can be any type — it must be a boolean expression in Java.

context