Table of Contents
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
1 2 3 4 5 |
do { // Statements to execute } while (condition); |
- 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 tofalse
, the loop ends, and control passes to the line of code immediately following the loop.
Here is the flow diagram for illustration:
3. Basic Do While Loop Example
Let’s consider a simple example where our goal is to print numbers from 1 to 5.
1 2 3 4 5 6 7 |
int i = 1; // Initialization do { System.out.println("i = " + i); // Statement to execute i++; // Increment } while (i <= 5); // Condition |
1 2 3 4 5 6 7 |
i = 1 i = 2 i = 3 i = 4 i = 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:
1 2 3 4 5 6 7 |
int i = 10; // Initialization do { System.out.println("i = " + i); // Statement to execute i++; // Increment } while (i <= 5); // Condition |
1 2 3 |
i = 10 |
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:
1 2 3 4 5 |
do { // Your code here will run endlessly } while (true); |
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.
1 2 3 4 5 6 7 |
int count = 5; while (count < 5) { System.out.println("This line won't execute because count is not less than 5"); count++; } |
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.
1 2 3 4 5 6 7 |
int count = 5; do { System.out.println("This line will execute once even if the count is not less than 5"); count++; } while (count < 5); |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package org.arpit.java2blog; public class DoWhileLoopMain { public static void main(String[] args) { DoWhileLoopMain dwl=new DoWhileLoopMain(); int arr[] ={32,45,53,65,43,23}; System.out.println(dwl.findElementInArr(arr, 53)); } public String findElementInArr(int arr[],int elementTobeFound) { int i=0; do { if(arr[i]==elementTobeFound) { System.out.println(elementTobeFound+" is present in the array "); return "PRESENT"; } i++; }while(i<arr.length); return "NOT PRESENT"; } } |
1 2 3 4 |
53 is present in the array PRESENT |
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.