Software Development Exam  >  Software Development Notes  >  Basics of Java  >  DO While Loop in Java

DO While Loop in Java | Basics of Java - Software Development PDF Download

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.

Syntax of the Do-While Loop

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:

  • The variable i is initially assigned a value of 1.
  • The code block within the do-while loop is executed, which prints the value of i and increments it by 1.
  • After each iteration, the condition i <= 5 is checked.
  • As long as i is less than or equal to 5, the loop continues executing. Once i becomes 6, the condition becomes false, and the loop terminates.

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:

  • In this example, we use two variables: num to keep track of the current number and sum to store the sum of all numbers.
  • Inside the do-while loop, we add the value of num to sum and then increment num by 1.
  • The loop continues until num is no longer less than or equal to 10.
  • Finally, we print the value of sum, which gives us the sum of numbers from 1 to 10.

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:

  • In this example, we use the Scanner class to read user input from the console.
  • Inside the do-while loop, we prompt the user to enter a positive number and store it in the number variable.
  • The loop continues until the condition number <= 0 becomes false, indicating that the user has entered a positive number.
  • Finally, we display the entered number on the console.

Sample Problems

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.

Conclusion

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.

The document DO While Loop in Java | Basics of Java - Software Development is a part of the Software Development Course Basics of Java.
All you need of Software Development at this link: Software Development
60 videos|37 docs|12 tests
60 videos|37 docs|12 tests
Download as PDF
Explore Courses for Software Development exam
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Download the FREE EduRev App
Track your progress, build streaks, highlight & save important lessons and more!
Related Searches

Extra Questions

,

DO While Loop in Java | Basics of Java - Software Development

,

pdf

,

past year papers

,

DO While Loop in Java | Basics of Java - Software Development

,

Important questions

,

Previous Year Questions with Solutions

,

Viva Questions

,

video lectures

,

MCQs

,

Exam

,

mock tests for examination

,

Sample Paper

,

ppt

,

Summary

,

Free

,

shortcuts and tricks

,

study material

,

Semester Notes

,

Objective type Questions

,

DO While Loop in Java | Basics of Java - Software Development

,

practice quizzes

;