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

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 loop in Java

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

Do keyword in Java

While keyword in Java

do keyword in Java

 

Suggested for you

Nested While in Java

Nested while in C

Nested while in C++

Nested while in Python

Nested do while in Java

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

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

1 month ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago