Signup/Sign In
PUBLISHED ON: APRIL 22, 2021

Java Do While Programs

In this tutorial, we will learn how to implement a do-while loop in different scenarios. But before moving further, if you are not familiar with the concept of the do-while loop, then do check the article on Loops in Java.

Syntax:

do
{  
//code to be executed  
}while(condition);  

Program 1: Java Program to Implement do-while Loop

In this program, how to find the sum of all the entered positive numbers using a do-while loop.

Algorithm:

  1. Start
  2. Create an instance of the Scanner class.
  3. Declare a variable.
  4. Ask the user to initialize the variable.
  5. Declare another variable to store the sum of all the positive numbers.
  6. Initialize it to 0.
  7. Use a do-while loop to check whether the entered numbers are positive or not.
  8. Increment the sum each time a positive number is entered.
  9. Break the loop if any negative number is entered.
  10. Display the sum.
  11. Stop.

Below is the code for the same.

// Java program to find the sum of positive numbers using do-while loop
import java.util.Scanner;

public class Main 
{
  public static void main(String[] args) 
  {
    // Take input from the user
    // create an object of Scanner class
    Scanner sc = new Scanner(System.in);
	   
    int sum = 0;
    int num = 0;

    // do...while loop continues 
    // until entered number is positive
    do {
      // add only positive numbers
      sum += num;
      System.out.println("Enter a number");
      num = sc.nextInt();
    } 
    while(num >= 0); 
	   
    System.out.println("The sum of entered positive numbers is " + sum);
    sc.close();
  }
}


Enter a number: 4
Enter a number: 6
Enter a number: 2
Enter a number: 8
Enter a number: 5
Enter a number: 1
Enter a number: 3
Enter a number: -9
The sum of entered positive numbers is 29

Program 2: Java Program to Implement do-while Loop

In this program, we will see how to implement a do-while loop program in java. Here, we will consider a scenario where we will find the multiplication table of a particular number. Instead of writing the multiplication table for each element, we will use a do-while loop for the same. We will write the statement once and it will be implemented multiple times.

Algorithm:

  1. Start
  2. Create an instance of the Scanner class.
  3. Declare a number
  4. Ask the user to initialize the number.
  5. Use a do-while loop to print the multiplication table of that number.
  6. Display the result.
  7. Stop.

Below is the code for the same.

//Java Program to see the multiplication table using a do-while loop program
import java.util.*;

public class Main
{
     public static void main(String []args)
     {
        //Take input from the user
        //Create instance of the Scanner Class
        Scanner sc=new Scanner(System.in); 
        System.out.println("Enter the number: ");
        int n=sc.nextInt();      //Declare and initialize the number
        int i=1;
        System.out.println("The multiplication table of "+n+" is: ");
        //Infinite Loop Example      
        do
        {
           System.out.println(n+" * "+i+" = "+ (n*i));
            i++;
        }
        while(i<=10);
     }
}


Enter the number: 4
The multiplication table of 4 is:
4 * 1 = 4
4 * 2 = 8
4 * 3 = 12
4 * 4 = 16
4 * 5 = 20
4 * 6 = 24
4 * 7 = 28
4 * 8 = 32
4 * 9 = 36
4 * 10 = 40

Program 3: Java Program to Implement do-while Loop

In this program, we will see how to use a do-while loop to perform a certain task infinite times. In order to do so, we will pass true in the condition statement of the while loop. Doing so will make it an infinitive do-while loop. A point to be noted here is that, in order to exit from an infinite loop, you need to press ctrl+c.

Algorithm:

  1. Start
  2. Declare a variable.
  3. Initialize it to 1.
  4. Use a do-while loop to print the message.
  5. Pass true in the condition of the while loop.
  6. Execute the statement until the condition is false.
  7. Increment the variable in each iteration.
  8. Display the result.
  9. Stop.

Below is the code for the same.

//Java Program to see the implementation of an infinite do-while loop program

public class Main
{
     public static void main(String []args)
     {
        int i=1;
        System.out.println("Example of Infinite do while loop: ");
        //Infinite Loop Example      
        do
        {
           System.out.println(i+" Hello World!");
            i++;
        }
        while(true);
     }
}


Example of Infinite do-while loop:
1 Hello World!
2 Hello World!
3 Hello World!
4 Hello World!
5 Hello World!
6 Hello World!
7 Hello World!
ctrl+c



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.