Skip to main content

Section 4.3 While and Do-While Loops

You’ve seen for loops for cases where you know (or can easily express) the number of iterations you need. But sometimes, you want to keep looping as long as a certain condition remains true—without necessarily knowing how many times that will be. In Java, there are two “indefinite loops” for that scenario: while and do-while. They both run until their condition becomes false, but they differ slightly in when the condition is checked.

Subsection 4.3.1 The while Loop

A while loop checks its condition before each iteration. The structure is:
while (condition) {
    // Loop body statements
}
Consider this example that prints numbers 0 through 4:
Notice we have the same four elements as a for loop: initialization (i = 0), condition (i < 5), the loop body, and an update (i++). One common pitfall is forgetting the update, which can cause an infinite loop.

Subsection 4.3.2 The do-while Loop

Unlike while, a do-while loop checks the condition after running the body once. This guarantees the loop body runs at least once, no matter what. Its syntax is:
do {
    // Loop body statements
} while (condition);
Here’s a simple version that again prints numbers 0 through 4. Notice the condition is tested at the end:
Watch out for the semicolon that follows while(...) in a do-while loop—it’s mandatory, and forgetting it leads to a compile error.

Subsection 4.3.3 Choosing Between while and do-while

While both loops handle indefinite iteration, choosing the right one improves code clarity. Consider these guidelines:
  • Use while when the loop might not need to run at all. For example, processing user input until a valid entry is provided.
  • Use do-while when the loop body must execute at least once. For example, displaying a menu and waiting for user selection.
Readability is enhanced when the loop structure matches the logical flow. A do-while clearly signals that the code inside runs once before checking, reducing cognitive load.
Example: Consider input validation. A while loop checks before prompting:
// Using while
System.out.print("Enter positive number: ");
int num = in.nextInt();
while (num <= 0) {
    System.out.print("Invalid. Enter positive: ");
    num = in.nextInt();
}
A do-while isn’t suitable here as we don’t want to process invalid input. Conversely, for a menu:
// Using do-while
char choice;
do {
    displayMenu();
    choice = in.next().charAt(0);
    processChoice(choice);
} while (choice != 'Q');
The do-while ensures the menu is displayed at least once, improving readability.

Subsection 4.3.4 Interactive Exercises: While & Do-While

Let’s solidify your understanding with a few practice exercises. Each focuses on fixing or writing a small loop, highlighting common usage patterns or pitfalls.

Checkpoint 4.3.1.

Exercise 1: The while loop below is missing an update to count and thus loops forever. Modify it to print exactly these numbers: 0, 1, 2, and 3.

Checkpoint 4.3.2.

Exercise 3: The code below aims to print "Hello!" exactly once using a do-while loop. But it’s missing the correct update or condition. Fix it so it prints "Hello!" once and stops.

Subsection 4.3.5 Summary: While and Do-While Loops

You now have two more loop constructs for situations where you don’t know up front how many times you’ll loop:
  • while: Checks its condition first. If it’s false from the start, the body never runs.
  • do-while: Checks its condition after the body, so it always runs at least once.
Both rely on an effective update step to avoid infinite loops. Next up, we’ll learn how to combine these loops with arrays to handle sets of data more elegantly.
You have attempted of activities on this page.