do while Loop in Java Programming
Loops in Java
In this lesson, we will understand what is do while loop and how it is used in Java programming.
What is do while Loop
A do while loop in Java programming is almost similar to while loop with an only difference that the test condition is checked at the end of the loop. A do while loop is also known as Exit Controlled loop because the test condition is evaluated, after the execution of the body of the do while loop.
In the do while loop, the body of the loop will execute at least once even after the test condition is false.
Syntax of do while Loop
initialization expression;
do
{
// body of the loop
// statements we want to execute
update_expression;
}
while(test_expression);
Example
public class Example
{
public static void main(String args[])
{
// initialization expression
int i = 1;
// test expression
do
{
System.out.println("Hello World");
// update expression
i=i+1;
}
while(i<=5);
}
}
Output
Hello World Hello World Hello World Hello World Hello World
We initialize the loop variable before starting the do while loop. After initialization, we will start the loop with a do statement. After that body of the do while loop is executed and the update expression is performed and at last test condition of do while is evaluated. When the test expression evaluates to false the do while loop terminates.
Note: There must be a semicolon ; after the end of while statement as shown in the above example.
Example 1
Java program to print the numbers from 1 to 10 on the screen using do while loop.
public class Example
{
public static void main(String args[])
{
int i=1;
do
{
System.out.println(i);
i=i+1;
}
while(i<=10);
}
}
Output
1 2 3 4 5 6 7 8 9 10
In the above example, we have run a do while loop from 1 to 10. Each time when the loop runs, we print the value of the variable i on the screen on a separate line using System.out.println() statement. The loop ends when the value of i is more than 10.
Example 2
Java program to print all the even numbers from 10 to 20 on the screen using do while loop.
public class Example
{
public static void main(String args[])
{
int i=10;
do
{
System.out.println(i);
i=i+2;
}
while(i<=20);
}
}
Output
10 12 14 16 18 20
In the above example, we have run a do while loop from 10 to 20. Each time when the loop runs, we print the value of the variable i on the screen on a separate line using System.out.println() statement and then increment the value of i by 2. The loop ends when the value of i is more than 20.
Example 3
Java program to print all the numbers from 10 to 1 in reverse order on the screen using do while loop.
public class Example
{
public static void main(String args[])
{
int i=10;
do
{
System.out.println(i);
i=i-1;
}
while(i>=1);
}
}
Output
10 9 8 7 6 5 4 3 2 1
In the above example, we have run a do while loop from 10 to 1 in reverse order. Each time when the loop runs, we print the value of the variable i on the screen on a separate line using System.out.println() statement and then decrement the value of i by 1. The loop ends when the value of i is less than 1.
Example 4
Java program to input a 4-digit number and find the sum of its digits using do while loop.
import java.util.Scanner;
public class Example
{
public static void main(String args[])
{
int n,r,sd=0;
Scanner sc=new Scanner(System.in);
System.out.print("Enter a 4 digit number ");
n=sc.nextInt();
if(n>=1000 && n<=9999)
{
do
{
r=n%10; // return the last digit of the number as remainder
sd=sd+r;
n=n/10; // removes the last digit of the number
}
while(n>0);
System.out.println("Sum of digits = " + sd);
}
else
{
System.out.println("Not a 4 digit number");
}
}
}
Output
Enter a 4 digit number 2461 Sum of digits = 13
In the above example, we have input a 4-digit number. The loop starts with the 4-digit number and runs till the value of n is greater than 0. Each time when the loop runs, we find the last digit of the number using n%10 and add the digit in the variable sd and remove the last digit from the number using n=n/10. The loop ends when the value of n becomes 0 after removing all the digits from n.
Nested do while Loop
Java programming allows using one do while loop inside another do while loop. This is known as nested do while loop.
Syntax of Nested do while Loop
initialization expression;
do
{
// body of the outer loop
// statements we want to execute
initialization expression;
do
{
// body of the inner loop
// statements we want to execute
update expression;
}
while(test expression);
update_expression;
}
while(test expression);
Example
public class Example
{
public static void main(String args[])
{
int i=1,j;
do
{
j=1;
do
{
System.out.print(j);
j++; // it means j=j+1
}
while(j<=i);
System.out.println();
i++; // it means i=i+1
}
while(i<=5);
}
}
Output
1 12 123 1234 12345
In the above example, we have run two loops, one outer loop and another inner loop. The outer loop runs from 1 to 5, and the inner loop runs from 1 to the current value of the outer loop (for example, when the value of i is 1, the inner loop runs from 1 to 1. When the value of i is 2, the inner loop runs from 1 to 2 and so on). The program ends when the outer loop test expression evaluates to false.
Infinite do while Loop
An infinite do while loop is a loop that never ends because its test condition never evaluates to false. It keeps on executing unless and until we terminate the loop using the break statement as shown in the example below.
Syntax of Infinite do while Loop in Java
do
{
// body of the infinite do while loop
}
while(true);
Example
public class Example
{
public static void main(String args[])
{
int i=0;
do
{
System.out.println("Hello World");
i=i+1;
if(i==5)
{
break; // terminate the infinite loop when the value of i is 5
}
}
while(true);
}
}
Output
Hello World Hello World Hello World Hello World Hello World