Skip to main content

#Do-while #loop You can understand Do-while loop only 5 minutes ||Loop c...





Do-while loop concepts it's very useful for the programmer the interview room so many questions ask through the interview



Java do while loop is used to execute a block of statements continuously until the given condition is true. do while loop in Java is similar to while loop except that the condition is checked after the statements are executed, so do while loop guarantees the loop execution at least once.

Table of Contents
[
hide
]

1 Java do while loop
1.1 do while java flow diagram
1.2 Java do while loop example
1.3 do while true java
1.4 do while vs while loop
Java do while loop
Java do while loop syntax is as follows:


do {
// statements
} while (expression);
The expression for the do-while loop must return a boolean value, otherwise, it will throw compile time error.

do while java flow diagram
Java Do While Loop
Java Do While Loop

Java do while loop example
Here is a simple java do while loop example to print numbers from 5 to 10.



package com.journaldev.javadowhileloop;

public class JavaDoWhileLoop {

public static void main(String[] args) {

int i = 5;
do {
System.out.println(i);
i++;
} while (i <= 10);
}
}
java do while loop example

do while true java
We can create an infinite loop by passing boolean expression as true in the do while loop.


Here is a simple do while java infinite loop example.


package com.journaldev.javadowhileloop;

public class DoWhileTrueJava {

public static void main(String[] args) throws InterruptedException {
do {
System.out.println("Start Processing inside do while loop");
// look for a file at specific directory
// if found, then process it, such as inserting rows into database
System.out.println("End Processing of do while loop");

Thread.sleep(5 * 1000);
} while (true);
}
}
Note that you will have to manually quit the application to stop it, using Ctrl+C if program is executed in terminal. If you have executed program in Eclipse IDE then there is a red color button to terminate the program.

do while vs while loop
The only time you should use do while loop is when you want to execute the statements inside loop at least once, even though condition expression returns false. Otherwise it’s always better to use while loop.

Java while loop looks more cleaner than do while loop.

That’s all for java do while loop. You should also look into java for loop and java continue statement.

Reference: Oracle Documentation



Facebook
Twitter
WhatsApp
Reddit
Linkedin
Email
PREV

Java while loop

NEXT

Java BigDecimal


Pankaj
I love Open Source technologies and writing about my experience about them is my passion.

Follow Author

Comments

rupesh
says:
February 25, 2019 at 6:04 am
please give syntax for do while loop

Reply


Nikhil
says:
February 6, 2019 at 6:56 pm
hi Pankaj,

Can you please correct the “if condition is true arrow” in the image?

Reply

Pankaj
says:
February 6, 2019 at 9:40 pm
Ah, thanks for noticing that small mistake. I have corrected the image now.

Reply
Leave a Reply
Your email address will not be published. Required fields are marked *

Comment

Name *

Email *





Newsletter for You
Enter your email address here...

Logo Jdev
JournalDev is one of the most popular websites for Java, Python, Android, and related technical articles. Our tutorials are regularly updated, error-free, and complete. Every month millions of developers like you visit JournalDev to read our tutorials.

JournalDev was founded by Pankaj

Comments

Popular posts from this blog

How to Send OTP in Mobile Number | login with OTP mobile Number | How to send OTP in mobile no with Spring Boot APP

   ðŸ˜‚               Login with Mobile Number OTP ---------------------------------------------------------------------------- If you want to develop a project to log in with OTP mobile Number in Spring Boot Applications then this post for you. In this post, I am going to use some other service to send the OTP in mobile number. we have to use it in this project spring boot. we are going to use Twilio to send SMS. we are going to use a web socket to send the data from the browser to the SMS gateway. Oracle Database for store the user details. <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> <version>2.3.3.RELEASE</version> </dependency> <dependency> <groupId>com.twilio.sdk</grou

Spring Boot With MySQL Database connection with Examples | MySQl Database Configuration with Spring Boot Projects

 ðŸ˜ƒ MySQL Database Configuration with Spring Boot Projects  In this article, we are going to introduce How to connect MySQL Database with the Spring Boot project. pom.xml   <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</gro

How can we create Auto generated field or ID for mongodb using spring boot

😂 How can we create an Auto-generated field or ID for MongoDB using spring boot? First Create One Application Like Mongodb_sequence-id-generator Pom.XML <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> <dependency> <groupId>de.flapdoodle.embed</groupId> <artifactId>de.flapdoodle.embed.mongo</artifactId> </dependency> User.java package com.app; import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Transient; import org.springframework.data.mongodb.core.mapping.Document; @Document(collection = "users_db") public class User { @Transient public static final String SEQUENCE_NAME = &