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:
- Start
- Create an instance of the Scanner class.
- Declare a variable.
- Ask the user to initialize the variable.
- Declare another variable to store the sum of all the positive numbers.
- Initialize it to 0.
- Use a do-while loop to check whether the entered numbers are positive or not.
- Increment the sum each time a positive number is entered.
- Break the loop if any negative number is entered.
- Display the sum.
- 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:
- Start
- Create an instance of the Scanner class.
- Declare a number
- Ask the user to initialize the number.
- Use a do-while loop to print the multiplication table of that number.
- Display the result.
- 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:
- Start
- Declare a variable.
- Initialize it to 1.
- Use a do-while loop to print the message.
- Pass true in the condition of the while loop.
- Execute the statement until the condition is false.
- Increment the variable in each iteration.
- Display the result.
- 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