What is a Do while loop in Java and how to use it?

Kavya Tolety
Edureka
Published in
4 min readJul 31, 2019

The Java do-while loop is used to iterate a set of statements until the given condition is satisfied. If the number of iteration is not fixed and you MUST have to execute the loop at least once, then use a do-while loop. In this article, you will learn all about the do-while loop in Java and how to use it.

Let us dive into the article and explore the following topics:

  • What is Do while loop in Java
  • Flow diagram
  • Syntax
  • Implementation through example
  • Infinite do-while loop
  • Syntax
  • Example of infinitive do-while loop

Let’s begin!

What is Do while loop in Java?

The do-while loop is similar to while loop, but it possesses one dissimilarity: In while loop, the condition is evaluated before the execution of loop’s body but in do-while loop condition is evaluated after the execution of loop’s body.

I will make the concept visually clear through this flow diagram.

Flow diagram:

Let me explain the above-shown diagram:

  1. First of all, it will execute a set of statements that are mentioned in your ‘do’ block.
  2. After that, it will come to the ‘while’ part where it checks the condition.
  3. If the condition is true, it goes back and executes the statements.
  4. If the condition is false, it directly exits the loop.

Now, moving on towards the syntax of do-while loop in Java

Syntax:

do{

//code to be executed

}while(condition);

As you can see, the syntax is pretty easy. Next, let me show you the implementation of the do-while loop through an example.

Implementation through an example:

public class Example {

public static void main(String args[]) {
int x = 1;

do {
System.out.print("value of x : " + x );
x++;
System.out.print("n");
}while( x < 11 );
}
}

Output:

value of x : 1
value of x : 2
value of x : 3
value of x : 4
value of x : 5
value of x : 6
value of x : 7
value of x : 8
value of x : 9
value of x : 10

In the above-mentioned code, you can see that the do while loop will execute the condition once, for sure.

Now, let’s hop onto our next segment, Infinite do-while loop in Java.

Infinite do-while loop in Java

The infinite do-while loop in Java is similar to the infinite while loop.

If you pass ‘true’ in the do-while loop, it will be an infinitive do-while loop. Here is the syntax:

Syntax:

do{

//code to be executed

}while(true);

Below is a simple example depicting the usage of the java infinitive do-while loop.

Example of infinitive do-while loop:

public class DoWhileInfinite {  
public static void main(String[] args) {
do{
System.out.println("infinitive do while loop");
}while(true);
}
}

Output:

infinitive do while loop
infinitive do while loop
infinitive do while loop

In order to exit the loop, press ctrl+c.

With this, I have reached the end of my blog. I hope the data added value to your Java knowledge. We will continue digging into more concepts of the Java world. Stay tuned!If you wish to check out more articles on the market’s most trending technologies like Artificial Intelligence, DevOps, Ethical Hacking, then you can refer to Edureka’s official site.

Do look out for other articles in this series that will explain the various other aspects of Java.

1. Object Oriented Programming

2. Inheritance in Java

3. Polymorphism in Java

4. Abstraction in Java

5. Java String

6. Java Array

7. Java Collections

8. Java Threads

9. Introduction to Java Servlets

10. Servlet and JSP Tutorial

11. Exception Handling in Java

12. Advanced Java Tutorial

13. Java Interview Questions

14. Java Programs

15. Kotlin vs Java

16. Dependency Injection Using Spring Boot

17. Comparable in Java

18. Top 10 Java frameworks

19. Java Reflection API

20. Top 30 Patterns in Java

21. Core Java Cheat Sheet

22. Socket Programming In Java

23. Java OOP Cheat Sheet

24. Annotations in Java

25. Library Management System Project in Java

26. Trees in Java

27. Machine Learning in Java

28. Top Data Structures & Algorithms in Java

29. Java Developer Skills

30. Top 55 Servlet Interview Questions

31. Top Java Projects

32. Java Strings Cheat Sheet

33. Nested Class in Java

34. Java Collections Interview Questions and Answers

35. How to Handle Deadlock in Java?

36. Top 50 Java Collections Interview Questions You Need to Know

37. What is the concept of String Pool in Java?

38. What is the difference between C, C++, and Java?

39. Palindrome in Java- How to check a number or string?

40. Top MVC Interview Questions and Answers You Need to Know

41. Top 10 Applications of Java Programming Language

42. Deadlock in Java

43. Square and Square Root in Java

44. Typecasting in Java

45. Operators in Java and its Types

46. Destructor in Java

47. Binary Search in Java

48. MVC Architecture in Java

49. Hibernate Interview Questions And Answers

Originally published at https://www.edureka.co on July 31, 2019.

--

--

Kavya Tolety
Edureka
Writer for

Hey! Just a normal girl on the internet who loves to read, research and analyze