Do While Loop in Java with Example

1. Introduction

In programming, controlling the flow of execution is essential for building functional and efficient applications. Java provides several control flow statements, and among these, the do-while loop offers a unique way to execute a block of code at least once and then repeat the execution based on a given condition.

2. The Syntax of Do While Loop

  • Statements to execute: This is the body of the loop where your code goes. This part will run at least once regardless of the condition at the end of the loop.
  • Condition: This is a boolean expression that is evaluated after the execution of the loop’s body. If the condition evaluates to true, the loop will run again. If it evaluates to false, the loop ends, and control passes to the line of code immediately following the loop.

Here is the flow diagram for illustration:

Do While Loop Flow Diagram in Java

3. Basic Do While Loop Example

Let’s consider a simple example where our goal is to print numbers from 1 to 5.

This example illustrates the do-while loop’s characteristic of executing the loop body first before checking the condition. Even if i were initialized to a value greater than 5, the statement inside the loop would still execute once.

Let’s initialize the i  to 10 and check the output:

As we can see, even if the condition was not true, the do-while loop executed at least once before terminating. This characteristic ensures that the loop’s body is run at least one time, making the do-while loop particularly useful for scenarios where the initial execution of the loop’s body is necessary regardless of the condition.

4. Infinite Do While Loop

An infinite loop occurs when the condition in a do-while loop always evaluates to true. This can happen due to several reasons, such as:

  • The loop condition is explicitly set to a boolean true.
  • The condition involves variables that are never modified within the loop, leading to a perpetual true state.
  • Logical errors in the loop’s design prevent the condition from ever becoming false.

Let’s illustrate an infinite loop with a simple example where the loop condition remains true indefinitely:

In this snippet, the condition in the while part of the loop is set to true, which is always true by definition. Therefore, the loop will never exit naturally, and the code block inside the do section will execute indefinitely. This can be useful for certain applications, such as a server that listens for incoming connections without stopping or a game loop that runs until the game is explicitly closed.

5. Difference between While Loop and Do While Loop

The main difference between a while loop and a do-while loop in Java lies in their execution and condition checking:

While Loop: In a while loop, the condition is checked before the execution of the loop’s body. If the condition evaluates to false at the very beginning, the body of the loop will not execute even once. This makes the while loop suitable for situations where you may not need to execute the loop at all, depending on the condition.

Do-While Loop: In contrast, a do-while loop checks the condition after the execution of the loop’s body. This guarantees that the loop’s body is executed at least once, even if the condition is false on the first check. This characteristic makes the do-while loop particularly useful for scenarios where the loop body needs to execute at least once regardless of the condition.

6. Exercise

Let’s apply our knowledge of while loops by writing a program.

Given an array of integers like {32, 45, 53, 65, 43, 23}, your task is to search for a specific element within this array. If the element exists, your program should output ‘PRESENT’; if it doesn’t, it should output ‘NOT PRESENT’.

I would recommend that you try it yourself first and then look at the code below:

7. Conclusion

The do-while loop in Java provides a powerful tool for executing a block of code repeatedly with the guarantee of at least one execution. Understanding its syntax and behavior, along with careful consideration of initialization, condition checks, and modifications within the loop, can help in effectively utilizing this control flow statement.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *