What is a Do while loop in Java and how to use it?
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:
- First of all, it will execute a set of statements that are mentioned in your ‘do’ block.
- After that, it will come to the ‘while’ part where it checks the condition.
- If the condition is true, it goes back and executes the statements.
- 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
5. Java String
6. Java Array
8. Java Threads
9. Introduction to Java Servlets
11. Exception Handling in Java
14. Java Programs
15. Kotlin vs Java
16. Dependency Injection Using Spring Boot
22. Socket Programming In Java
25. Library Management System Project in Java
26. Trees in Java
28. Top Data Structures & Algorithms in Java
30. Top 55 Servlet Interview Questions
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
Originally published at https://www.edureka.co on July 31, 2019.