Java while and do...while Loop


In Java, loops are essential control structures that allow you to execute a block of code repeatedly. While both the while loop and the do...while loop allow you to repeat a set of actions, they have slight differences in how they handle the loop's condition and when the loop executes. Understanding the differences between these two loops is key to using them effectively.

In this guide, we’ll explore both the while loop and the do...while loop, their syntax, differences, examples, and best practices.

Table of Contents

  1. What is a while Loop?
  2. Syntax of the while Loop
  3. What is a do...while Loop?
  4. Syntax of the do...while Loop
  5. Differences Between the while and do...while Loop
  6. Examples of while and do...while Loop
  7. Best Practices for Using while and do...while Loops

What is a while Loop?

A while loop in Java repeatedly executes a block of code as long as a specified condition remains true. The condition is evaluated before each iteration, which means if the condition is false from the beginning, the loop may not execute even once.


Syntax of the while Loop

The syntax of the while loop consists of a condition that is evaluated before each loop iteration. If the condition is true, the loop’s body will execute. If it is false, the loop will terminate.

while (condition) {
    // block of code to be executed
}
  • condition: A boolean expression that determines whether the loop should continue executing.
  • block of code: A set of instructions to be executed as long as the condition evaluates to true.

Example:

int i = 0;
while (i < 5) {
    System.out.println(i);
    i++;
}

What is a do...while Loop?

A do...while loop in Java is similar to the while loop, but with one key difference: the condition is evaluated after the loop body is executed. This means the code inside the loop is guaranteed to run at least once, regardless of whether the condition is initially true or false.


Syntax of the do...while Loop

do {
    // block of code to be executed
} while (condition);
  • block of code: A set of instructions that will always execute at least once before checking the condition.
  • condition: A boolean expression that is evaluated after the loop executes.

Example:

int i = 0;
do {
    System.out.println(i);
    i++;
} while (i < 5);

Differences Between the while and do...while Loop

Feature while Loop do...while Loop
Condition Check Before the loop starts (Pre-test) After the loop executes (Post-test)
Guaranteed Execution May not execute at all if the condition is false initially. Always executes at least once.
Common Use Case Used when the number of iterations is not known beforehand and the condition should be checked first. Used when the loop should run at least once, regardless of the condition.

Examples of while and do...while Loop

Example 1: Printing Numbers Using while Loop

In this example, we will print numbers from 1 to 5 using a while loop.

public class WhileLoopExample {
    public static void main(String[] args) {
        int i = 1;
        
        // Using while loop to print numbers 1 to 5
        while (i <= 5) {
            System.out.println(i);
            i++;
        }
    }
}

Explanation:

  • The condition i <= 5 is checked before the loop executes. If the condition is true, the loop body is executed.
  • The variable i is incremented in each iteration until it reaches 6, which causes the loop to stop.

Output:

1
2
3
4
5

Example 2: Printing Numbers Using do...while Loop

Now, let’s use the do...while loop to print numbers from 1 to 5.

public class DoWhileLoopExample {
    public static void main(String[] args) {
        int i = 1;
        
        // Using do...while loop to print numbers 1 to 5
        do {
            System.out.println(i);
            i++;
        } while (i <= 5);
    }
}

Explanation:

  • The block of code is executed first, then the condition i <= 5 is checked.
  • As long as i <= 5 is true, the loop will continue running.

Output:

1
2
3
4
5

Best Practices for Using while and do...while Loops

  1. Use while loop when the condition might not be true initially: If you need to ensure the loop runs only when a condition is true, a while loop is appropriate.
  2. Use do...while loop for guaranteed execution: When you need the code to execute at least once, such as user input validation, the do...while loop is ideal.
  3. Avoid infinite loops: Always ensure that the loop’s condition will eventually become false, or you will end up with an infinite loop.
  4. Increment or update loop variables correctly: Make sure that the loop’s variables are updated properly inside the loop to avoid unexpected behavior.