Do-while Loop in Java
A do-while loop is used to execute a block of code at least once and then repeatedly as long as a specified condition is true.
It checks the condition after executing the loop body, ensuring the loop body is executed at least once regardless of whether the condition is initially true or false.
It is a control structure in Java that allows you to execute a block of code at least once and then repeatedly as long as a given condition is true.
The do-while loop is similar to the while loop but guarantees that the loop body is executed at least once.
Syntax of a do-while Loop
Here is the basic syntax of a do-while loop in Java:
do {
// code to be executed
} while (condition);
do: This keyword marks the beginning of the loop.
code to be executed: The block of code that you want to execute.
while (condition): The condition that is evaluated after each iteration of the loop. If the condition is true, the loop body is executed again. If the condition is false, the loop terminates.
How It Works
Loop Body Execution: The block of code inside the do block is executed once unconditionally.
Condition Check: The while condition is evaluated after the loop body has been executed.
Repeat: If the condition is true, the loop body is executed again. If the condition is false, the loop terminates.
This process repeats until the condition evaluates to false.
Example of a do-while Loop
Let's look at a simple example to understand how a do-while loop works. Consider the following code:
public class Main {
public static void main(String[] args) {
int i = 0;
do {
System.out.println("Iteration: " + i);
i++;
} while (i < 5);
}
}
Explanation:
Initialization: int i = 0; initializes the counter variable i to 0.
Loop Body: The code inside the do block is executed, printing the current value of i and then incrementing i by 1.
Condition Check: i < 5 is the condition that is checked after the loop body is executed. If true, the loop body executes again. If false, the loop terminates.
Since i starts at 0 and increments by 1 each time, the loop will run 5 times, producing the following output:s
Iteration: 0
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Using a do-while Loop with User Input
A common use case for a do-while loop is to prompt the user for input at least once and repeat the prompt until valid input is received. Here's an example:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int number;
do {
System.out.print("Enter a number between 1 and 10: ");
number = scanner.nextInt();
if (number < 1 || number > 10) {
System.out.println("Invalid input. Try again.");
}
} while (number < 1 || number > 10);
System.out.println("You entered: " + number);
}
}
Explanation:
Infinite Loop: The do-while loop ensures the prompt is displayed at least once.
User Input: number = scanner.nextInt(); reads an integer input from the user.
Condition Check: number < 1 || number > 10 checks if the input is outside the valid range.
Invalid Input: If the input is invalid, a message is printed, and the loop continues.
Valid Input: If the input is valid, the loop terminates.
Summary
do-while loop: Executes a block of code at least once and then repeatedly as long as a specified condition is true.
Syntax: do { // code } while (condition);
Loop Body: Code block executed at least once.
Condition: A boolean expression evaluated after each iteration.
Use Cases: Ideal for scenarios where the loop body must execute at least once, such as prompting for user input.
Common Pitfalls: Be mindful of infinite loops and ensure the loop condition can eventually become false.
The do-while loop is a versatile and essential control structure in Java.