- Java Tutorial
- Java Introduction
- Java Features
- Java Simple Program
- JVM, JDK and JRE
- Java Syntax
- Java Comments
- Java Keywords
- Java Variables
- Java Literals
- Java Separators
- Java Datatypes
- Java Operators
- Java Statements
- Java Strings
- Java Arrays
- Control Statement
- Java If
- Java If-else
- Java If-else-if
- Java Nested If
- Java Switch
- Iteration Statement
- Java For Loop
- Java For Each Loop
- Java While Loop
- Java Do While Loop
- Java Nested Loop
- Java Break/Continue
- Java Methods
- Java Methods
- Java Method Parameters
- Java Method Overloading
- Java Recursion
- Java OOPS
- Java OOPs
- Java Classes/Objects
- Java Inheritance
- Java Polymorphism
- Java Encapsulation
- Java Abstraction
- Java Modifiers
- Java Constructors
- Java Interface
- Java static keyword
- Java this keyword
- Java File Handling
- Java File
- Java Create File
- Java Read/Write File
- Java Delete File
- Java Program To
- Add Two Numbers
- Even or Odd Numbers
- Reverse a String
- Swap Two Numbers
- Prime Number
- Fibonacci Sequence
- Palindrome Strings
- Java Reference
- Java String Methods
- Java Math Methods
Java Do-While Loop
In Java, the do-while loop is a "post-test" loop. While a standard while loop checks its condition before running any code, the do-while loop flips that logic on its head. It ensures that the code block inside the loop executes at least once, regardless of whether the condition is true or false when the program reaches it.
Think of it like a "try before you buy" approach: the program performs the action first, then decides if it needs to repeat that action based on the result.
do-while loop when you need to perform an action (like showing a menu or asking for input) before you have the data needed to check the exit condition.
Syntax:
do {
// Code to execute
// This block runs at least once
} while (condition);
while loop, the do-while loop must end with a semicolon after the closing parenthesis of the condition. Forgetting this is a very common syntax error.
Code Execution:
The flow of a do-while loop follows a specific sequence that guarantees initial execution:
- Step 1: The program enters the
doblock and executes all code inside the curly braces. - Step 2: After the block finishes, the program evaluates the expression inside the
while(condition). - Step 3: If the condition is true, the program jumps back to the top of the
doblock and repeats the process. - Step 4: If the condition is false, the loop terminates, and the program continues with the next line of code after the loop.
Do-While Loop Example:
Let's look at a simple example where we print a countdown. Even if we started with a number that already failed the condition, the first number would still print.
int count = 1;
do {
System.out.println("Count is: " + count);
count++;
} while (count <= 5);
In this case, the code prints numbers 1 through 5. If we had initialized count to 10, the program would still print "Count is: 10" once before checking the condition and exiting.
Use Cases:
While for and while loops are more common in daily coding, do-while shines in specific real-world scenarios:
- User Input Validation: You want to ask a user for a password or a specific number. You must ask them at least once before you can check if their input is valid.
- Menu-Driven Applications: In console apps, you often want to display a list of options (1. Start, 2. Settings, 3. Exit), perform the user's choice, and then decide whether to show the menu again based on whether they chose "Exit."
- Game Loops: Ensuring the first frame of a game or a round of a "Guess the Number" game renders before checking if the "Game Over" state has been triggered.
do-while block focused. If the block becomes too large (e.g., 50+ lines), consider moving that logic into a separate method to keep your loop readable.
Summary
The do-while loop is a specialized tool in a Java developer's kit. It provides a guaranteed execution of a code block followed by a conditional check. By understanding that the condition is checked after the work is done, you can write cleaner logic for user interactions and input processing without needing to duplicate code before the loop starts.