🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
In Java, loops are used to execute a set of statements repeatedly until a particular condition is satisfied. The while loop and the do-while loop are two forms of conditional loops that serve this purpose.
A while loop evaluates its expression at the beginning of each loop cycle, so the code inside the loop may not execute at all if the condition is not met.
A do-while loop evaluates its expression at the end of each loop cycle, which guarantees that the code inside the loop will execute at least once.
2. Key Points
1. A while loop checks the condition before the code within the loop is executed.
2. A do-while loop checks the condition after the code within the loop is executed.
3. As a result of the check timing, a while loop may not execute its code at all if the condition is initially false.
4. A do-while loop always executes its code block at least once.
3. Differences
| while loop | do-while loop |
|---|---|
| The while loop checks the condition at the beginning of the loop. | The do-while loop checks the condition at the end of the loop. |
| If the condition is false at the first iteration, the loop body does not execute even once. | The loop body is guaranteed to execute at least once, regardless of the condition. |
| More commonly used when the number of iterations is unknown and depends solely on the condition. | More suitable when the loop body needs to be executed at least once, such as when displaying a menu or prompting for user input. |
| If the condition is initially false, It can result in zero loop iterations. | Always results in at least one iteration, even if the condition is initially false. |
| Syntax: while (condition) { statements; } | Syntax: do { statements; } while (condition); |
4. Example
public class LoopComparison {
public static void main(String[] args) {
int count = 0;
// Step 1: Using a while loop
System.out.println("while loop output:");
while (count < 0) {
System.out.println("This will not print");
}
// Step 2: Using a do-while loop
System.out.println("do-while loop output:");
do {
System.out.println("This will print at least once");
count++;
} while (count < 0);
}
}
Output:
while loop output: do-while loop output: This will print at least once
Explanation:
1. The while loop with the condition count < 0 does not execute because the condition is false at the start.
2. The do-while loop, on the other hand, executes once before checking the condition count < 0, which is why the statement within the loop's body is printed.
5. When to use?
- Use a while loop when you want to execute code only if the condition is true at the start of the loop.
- Use a do-while loop when you want to ensure that the code within the loop executes at least once, regardless of the condition being true or false.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment