In Java, if you require a loop that must execute at least once before evaluating a condition, you would utilize the do-while loop. This loop verifies the condition after executing the loop body.
What is do-while Loop in Java?
In Java, the do-while loop is employed to repeatedly execute a section of the program until a specific condition becomes true. When the number of iterations is indefinite and it is necessary to run the loop at least once, utilizing a do-while loop is advisable.
In Java programming, the do-while loop is known as an exit control loop. In contrast to the while loop and for loop, the do-while loop evaluates the condition after executing the loop body. This characteristic ensures that the Java do-while loop runs at least once since the condition is verified after the loop body has been executed.
Syntax of do-while Loop
It has the following syntax:
do{
//code to be executed / loop body
//update statement
}while (condition);
The components of a do-while loop include:
-
1.
- Condition: This is an expression that undergoes testing. When the condition evaluates to true, the loop body is executed, and then the control moves to the update expression. Once the condition turns false, the loop terminates automatically.
The condition is
- i is less than or equal to 100. The update expression modifies the loop variable each time the loop body is executed, either increasing or decreasing its value.
Note: The do block is executed at least once, even if the condition is false.
Flow chart of do-while loop
The provided illustration showcases the functionality of a do-while loop in the Java programming language.
Examples of do-while Loop
In this section, we will illustrate the usage of the do-while loop in Java through various examples.
Example 1: Printing Numbers from 1 to 10
In the following example, we demonstrate the printing of integer values ranging from 1 to 10. In contrast to the for loop, it is essential to initialize and increment the variable (in this case, i) separately in the while loop's condition to prevent the loop from running infinitely.
Example
//Simple do-while example in Java
public class Main {
public static void main(String[] args) {
//initialization
int i=1;
//do-while loop
do{
System.out.println(i);
i++;
}while(i<=10);
}
}
Output:
1
2
3
4
5
6
7
8
9
10
Example 2: Finding Factorial of a Number
Consider this example where we aim to calculate the factorial of a given number by employing a do-while loop in Java.
Example
// Java Program to print factorial of 5 using do-while loop
public class Main {
public static void main(String[] args) {
int number = 5;
int factorial = 1;
int i = 1;
//Start a do-while loop
do {
// Multiply the current value of 'factorial' by 'i' and store the result back in 'factorial'.
factorial *= i; // This is equivalent to factorial = factorial * i;
i++;
} while( i <= number );
// Print the calculated factorial to the console.
System.out.println("Factorial of " + number + " is: " + factorial);
}
}
Output:
Factorial of 5 is: 120
Java Infinitive do-while Loop
When true is passed into a do-while loop, it will create an infinite do-while loop.
Syntax of Infinitive do-while Loop
It has the following syntax:
do{
//code to be executed
}while(true);
Example 1: Java Infinitive do-while Loop
To illustrate the functionality of the do-while loop in Java, we will consider a different example.
public class DoWhileExample2 {
public static void main(String[] args) {
do{
System.out.println("infinitive do while loop");
}while(true);
}
}
Output:
infinitive do while loop
infinitive do while loop
infinitive do while loop
ctrl+c
Example 2: Java Infinitive do-while Loop
An illustration will now be presented to showcase how the infinite do-while loop functions in Java.
import java.util.Scanner;
public class DoWhileLoop {
public static void main(String[] args) {
// Creating a Scanner object for reading input from the user
Scanner sc = new Scanner(System.in);
// Declare variables to store user input and the s of numbers
int n;
int s = 0;
int c = 0; // Variable to c the n of iterations
// Display a welcome message
System.out.println("Welcome to the Do-While Loop Program!");
// Start a do-while loop
do {
// Prompt the user to enter a n
System.out.print("Please enter the value of a and n (or -1 to exit): ");
// Read the n entered by the user
n = sc.nextInt();
// Check if the n is not equal to -1 (exit condition)
if (n != -1) {
// Add the n to the s
s += n;
// Increment the c of iterations
c++;
}
// Continue looping until the user enters -1
} while (n != -1);
// Display the s of the numbers entered
System.out.println("Sum of the numbers entered: " + s);
// Display the n of iterations (excluding the exit condition)
System.out.println("Number of iterations: " + c);
// Calculate and display the average of the numbers entered
double average = (double) s / c;
System.out.println("Average of the numbers entered: " + average);
// Close the Scanner object to release system resources
sc.close();
}
}
Output:
Welcome to the Do-While Loop Program!
Please enter the value of a and n (or -1 to exit): 5
Please enter a n (or -1 to exit): -1
Sum of the numbers entered: 5
Number of iterations: 1
Average of the numbers entered: 5.0