100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Java

while and do-while Loops in Java

Learn the difference between Java's entry-controlled while loop and exit-controlled do-while loop, including syntax, semantics, and common pitfalls.

Control FlowBeginner8 min readJul 7, 2026
Analogies

1. Introduction

Java provides two loop constructs based on a single boolean condition without a built-in counter: the while loop and the do-while loop. Both repeat a block of code as long as a condition remains true, but they differ in when that condition is checked.

🏏

Cricket analogy: A bowler keeps bowling overs as long as the captain hasn't declared or the target isn't reached, but whether the review happens before or after the over decides if a wasted over is possible, just like while versus do-while.

The while loop is entry-controlled (pre-test): the condition is checked before the loop body runs, so the body may execute zero times. The do-while loop is exit-controlled (post-test): the condition is checked after the body runs, guaranteeing the body executes at least once.

🏏

Cricket analogy: The while loop is like an umpire checking the light meter before every over so an over is skipped entirely if it's too dark, while the do-while is like bowling one over first and only then checking if light permits another, guaranteeing at least one over.

2. Syntax

while loop

java
while (condition) {
    // loop body — may run zero times
}

do-while loop

java
do {
    // loop body — runs at least once
} while (condition);  // note the mandatory trailing semicolon

3. Explanation

In a while loop, if the condition evaluates to false on the very first check, the body never executes at all. This makes while loops suitable when it's uncertain whether the body should run even once, such as validating user input before any processing.

🏏

Cricket analogy: If the target score is already zero runs needed when the batting side walks out, like a Duckworth-Lewis washout scenario, the while loop's innings never even begins, avoiding a pointless over being bowled.

In a do-while loop, the body runs first, and the condition is evaluated afterward to decide whether to repeat. Because the check happens after execution, the body is guaranteed to run at least one time, which is useful for menu-driven programs or input validation loops that must present a prompt before checking a response.

🏏

Cricket analogy: A do-while loop is like a team batting one mandatory over first before the umpire checks if rain has stopped play, guaranteeing at least one over is bowled before any condition halts the match.

Exam trap: forgetting the trailing semicolon after while (condition) in a do-while loop is a compile error — unlike the while loop and for loop, the do-while statement must end with ; after the condition.

Both loops support break and continue for flow control, and both can become infinite loops if the condition never becomes false (e.g., while (true) { }).

4. Example

java
public class WhileDoWhileDemo {
    public static void main(String[] args) {
        // while loop: entry-controlled
        int count = 5;
        while (count < 3) {
            System.out.println("while body: " + count);
            count++;
        }
        System.out.println("After while loop, count = " + count);

        // do-while loop: exit-controlled, runs at least once
        int n = 5;
        do {
            System.out.println("do-while body: " + n);
            n++;
        } while (n < 3);
        System.out.println("After do-while loop, n = " + n);
    }
}

5. Output

text
After while loop, count = 5
do-while body: 5
After do-while loop, n = 6

6. Key Takeaways

  • while is entry-controlled: the condition is checked before the body runs, so it may execute zero times.
  • do-while is exit-controlled: the body runs first and the condition is checked after, guaranteeing at least one execution.
  • do-while requires a trailing semicolon after the condition — omitting it is a compile error.
  • Both loops can become infinite if the condition never turns false.
  • do-while is well suited to input-validation or menu loops that must run once before checking.
  • Both support break and continue for controlling iteration flow.

Practice what you learned

Was this page helpful?

Topics covered

#Java#JavaProgrammingStudyNotes#Programming#WhileAndDoWhileLoopsInJava#While#Loops#Syntax#Loop#StudyNotes#SkillVeris