Looping Constructs: For, While, and Do-While
Master the use of loops (for, while, do-while) to iterate and repeat code blocks. Implement repetitive tasks efficiently.
The Do-While Loop in Java
In Java, the do-while
loop is a looping construct that allows you to execute a block of code repeatedly. Unlike the while
loop, the do-while
loop guarantees that the block of code is executed at least once, regardless of the initial condition.
Syntax
The syntax of the do-while
loop is as follows:
do {
// Code to be executed
} while (condition);
Explanation
- The
do
keyword marks the beginning of the loop body. - The code within the curly braces
{}
is the block of code that will be executed. - After the execution of the code block, the
while
keyword is followed by a condition enclosed in parentheses()
. - The condition is a boolean expression that is evaluated after each iteration.
- If the condition is
true
, the loop continues. - If the condition is
false
, the loop terminates. - The semicolon
;
after thewhile
condition is mandatory. Forgetting it is a common error.
Guaranteed First Execution
The key difference between the do-while
loop and the while
loop is that the do-while
loop executes the code block before checking the condition. This ensures that the code within the loop is always executed at least once, even if the condition is initially false
.
When to Use the Do-While Loop
The do-while
loop is particularly useful when you need to execute a block of code at least once, regardless of the initial condition. Some common scenarios include:
- User Input Validation: Prompting the user for input and validating it within the loop, ensuring that the user is asked for input at least once.
- Menu-Driven Applications: Displaying a menu and allowing the user to select an option, even if they choose to exit immediately.
- Playing Games: Ensuring the game runs at least one round, before checking if the user wants to play again.
Example
Here's a simple example demonstrating the use of the do-while
loop:
public class DoWhileExample {
public static void main(String[] args) {
int count = 0;
do {
System.out.println("Count is: " + count);
count++;
} while (count < 5);
System.out.println("Loop finished!");
}
}
In this example, the loop will execute 5 times, printing the current value of count
in each iteration. The initial value of count
is 0, and the loop continues as long as count
is less than 5.
Another Example - Input Validation
import java.util.Scanner;
public class InputValidationExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int age;
do {
System.out.print("Enter your age (must be between 0 and 150): ");
age = scanner.nextInt();
if (age < 0 || age > 150) {
System.out.println("Invalid age! Please enter a valid age.");
}
} while (age < 0 || age > 150);
System.out.println("You entered a valid age: " + age);
scanner.close();
}
}
This example repeatedly prompts the user for their age until they enter a valid age (between 0 and 150). It *guarantees* they are prompted *at least* once.