When writing programs, it's often necessary to repeat a certain block of code multiple times until a specific condition is met. This is where loops come in handy. In Java, one such loop is the do-while loop. It's similar to the while loop, but with a slight difference in the execution order. In this article, we will explore the do-while loop, understand its syntax, and see various examples to grasp its functionality.
The syntax of the do-while loop in Java is as follows:
do {
// code to be executed
} while (condition);
The code block within the curly braces will be executed at least once, regardless of the condition. After the code block is executed, the condition is checked. If the condition evaluates to true, the code block is executed again. This process continues until the condition becomes false.
Example 1: Printing Numbers using a Do-While Loop
Let's start with a simple example to print numbers from 1 to 5 using a do-while loop:
int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 5);
Output
1
2
3
4
5
Explanation:
Example 2: Calculating the Sum of Numbers
Let's now consider a more practical example: calculating the sum of numbers from 1 to 10 using a do-while loop:
int num = 1;
int sum = 0;
do {
sum += num;
num++;
} while (num <= 10);
System.out.println("Sum: " + sum);
Output
Sum: 55
Explanation:
Example 3: Validating User Input
Another common use case for the do-while loop is to validate user input. Let's consider an example where we ask the user to enter a positive number:
import java.util.Scanner;
int number;
Scanner scanner = new Scanner(System.in);
do {
System.out.print("Enter a positive number: ");
number = scanner.nextInt();
} while (number <= 0);
System.out.println("You entered: " + number);
Output
Enter a positive number: -5
Enter a positive number: 0
Enter a positive number: 10
You entered: 10
Explanation:
Now that we have a good understanding of the do-while loop, let's try some sample problems to reinforce our knowledge:
1. Problem: Print the first 10 multiples of 3 using a do-while loop.
int i = 1;
do {
System.out.println(i * 3);
i++;
} while (i <= 10);
Output
3
6
9
12
15
18
21
24
27
30
2. Problem: Calculate the factorial of a given number using a do-while loop.
int number = 5;
int factorial = 1;
int i = 1;
do {
factorial *= i;
i++;
} while (i <= number);
System.out.println("Factorial of " + number + " is: " + factorial);
Output
Factorial of 5 is: 120
These sample problems illustrate the versatility of the do-while loop and how it can be used to solve different types of problems.
In this article, we explored the do-while loop in Java. We learned about its syntax and execution flow, and saw several examples to understand its practical usage. The do-while loop is a powerful construct for repeating code until a certain condition is met, making it an essential tool in Java programming. By practicing the provided examples and solving the sample problems, you'll gain a solid foundation in using the do-while loop effectively.
60 videos|37 docs|12 tests
|
|
Explore Courses for Software Development exam
|