In this tutorial, we will discuss do while loop in Java programming language
In the Java language, there are three types of basic looping statements
In the Java programming language, do- while loop is used for execution and evaluation of the body of Java code repeatedly until the test expression becomes false. The do-while loop is executed when the test expression is evaluated and the condition is satisfied. Do while loop is executed at least once before while the part is evaluated.
Do while loop contains two-part
The syntax of the do-while loop
do{ //statement(s) to be executed }while(condition);
flow diagram of while
According to the above diagram, initially, execution begins and flow of control enters the body of the do-while loop and statement is executed only once.
Then, the test expression is evaluated.
When the test expression is true, this process continues until the test expression become false
When test expression is false, the do-while loop stops the execution and goes to rest.
Program 1
public class DowhileEx{ public static void main(String args[]){ int i=1; do{ System.out.println("i is :"+i); i++; }while(i<=10); //when the test expression is true } }
When the above code is executed, it produces the following results:
i is :1 i is :2 i is :3 i is :4 i is :5 i is :6 i is :7 i is :8 i is :9 i is :10
In the above program, it displays 1- 10 positive numbers using the do-while loop
class CalcSum{ public static void main (String args[]){ int sum=0,i=1; do{ sum+=i; i++; }while(i<=100); System.out.println("The sum of 100 natural numbers (1 to 100) :"+sum); } }
When the above code is executed, it produces the following results:
The sum of 100 natural numbers (1 to 100 ):5050
In the above program, it calculates the sum of the 1 to 100 positive numbers using the do-while loop
import java.util.Scanner; class FindFactDoWhile{ public static void main(String args[]){ int n, fact=1; Scanner scan=new Scanner(System.in); System.out.println("Enter number for find factorial"); n=scan.nextInt(); int i=1; do { fact=fact*i; i++; }while(i<=n); System.out.println("factorial is :"+fact); } }
When the above code is executed, it produces the following results:
Enter number for find factorial 8 factorial is : 40320
In the above program, it calculates find factorials of given numbers using the do-while loop
class DoWhileArray{ public static void main(String args[]){ int marks[]={45,78,93,27}; //array index start in Zero int i=0; do{ System.out.println("Marks "+(i+1)+" :"+marks[i]); i++; }while(i<=3); } }
When the above code is executed, it produces the following results:
Marks 1 :45 Marks 1 :78 Marks 1 :93 Marks 1 :27
In the above program, it displays sequence of single dimension array elements using the do-while loop
public class DowhileEx{ public static void main(String args[]){ do{ System.out.println("Infinite do while loop"); }while(true); //when the test expression always true } }
When the above code is executed, it produces the following results:
Infinite do while loop Infinite do while loop Infinite do while loop Infinite do while loop Infinite do while loop Infinite do while loop Infinite do while loop Infinite do while loop ............... ............... To stop execution ctrl +c
Program 3
public class DowhileEx{ public static void main(String args[]){ do{ System.out.println("This loop is execute only once"); }while(false); //when the test expression always true } }
When the above code is executed, it produces the following results:
This loop is execute only once
In the above program, when test expression is false loop is executed only once.
There are other Java language keyword that is similar to the this post
Suggested for you
Nested for loop in C++ language
Nested for loop in Python language
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…