While loop

Do while loop in Java programming language

Do while loop in Java programming language

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

  • for loop
  • while loop
  • do while loop
Do while loop in Java

do while loop

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

  • Do part – Do part is a starting point of the do-while loop. do part is always executed.
  • While part– Do part is the processing point of the do-while loop.

 

Declaration

The syntax of the do-while loop

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

flow diagram of while

Do- while loop in Java

How do-while loop works:

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

Sum of 100 numbers using do while loop in Java

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

Find factorial using Do while loop in Java

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

Iterating array 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

Infinite do while loop in Java language

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

 

Do while loop always executes only one

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

Similar post

Hello world in Java

Hello world in C++

Hello world in Python

Hello world in C#

 

Suggested for you

for loop in Java

while loop in Java

for loop in C language

while loop in C language

while loop in cpp language

For loop in Cpp language

Nested for loop in C++ language

Nested for  in Java language

Nested for  loop in C language

Nested for loop in Python language

 

Do while loop in C programming language
Do while loop in Cpp programming language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago