The do while loop is another type of loop that executes a block of code once before checking the condition. The first thing that happens is the code inside the loop is executed. Then the condition is checked. If the condition is true, the code block will be executed again. This process continues until the condition is false.
The Do-While Loop is a type of program loop that executes a statement or a set of statements in a program one or more times, depending upon a given condition. The Do-While Loop is similar to the While-Loop as both of them evaluate the loop condition at the beginning of the loop execution. The difference between Do-While Loop and While-Loop is that the Do-While Loop executes the block of statements once, whereas the While-Loop executes the block of statements at least once. During the execution of the block of statements, the program evaluates the condition. If the condition is true, then the statement or statements are executed again. The program loops through the block of statements until the condition evaluates to false.
The do-while loop in Java is a variant of the while loop. This is the same as the while loop, except that the body of the loop is executed first, and then the loop continuation condition is evaluated.
In the while statement, the control condition is evaluated before the loop is executed. If the condition is initially false, the loop is not executed at all.
However, sometimes it is necessary to execute the body of the loop at least once before running the test. These situations can be handled by using the do-while loop in Java.
The do-while loop always executes its body at least once before evaluating the test, because the conditional test expression is at the end of the loop.
The general syntax of the do-while loop in Java is as follows:
// Initialization:
do {
// body of the loop;
statement(s);
increment/decrement;
} while (check the conditional expression) ;
A block diagram of the do-while loop in Java is shown in the following figure.
The flowchart of the do-while loop shows that first the body of the loop is executed, and then the conditional expression of the loop is evaluated to determine whether the loop should continue or end.
If the evaluation is true, the loop body is executed again. This process continues until the test condition becomes true.
If the specified condition becomes false, the loop is interrupted, the execution control exits the do-while loop, and continues with the next instruction, which appears immediately after the while instruction.
Since the specified condition is checked at the end of the loop, the do…while loop is an output-driven loop. The test condition must be a boolean expression.
We don’t need to use braces if there is only one statement in the do…while loop.
Let’s take the following example.
. . . . .
. . . . .
int i = 1, sum = 0;
do {
sum = sum + i;
i = i + 2;
}
while(sum < 40 || i < 10);
. . . .
. . . . .
In this example, the body of the loop is executed until one of the two relations becomes true.
Example of a program based on the do while loop in Java
1. Let’s take a simple example of a program in which we want to represent numbers from 1 to 6.
Program source code 1 :
package javaProgram;
public class Test {
public static void main(String[] args)
{
int x;
x = 1; // the starting number is 1.
do {
System.out.println(x); // print the value of x.
x++; // Increase the value of x by 1.
} while(x <= 6); // This instruction is executed as long as x <= 6.
}
}
a. In this example, the value of x is initially set to 1. Therefore, the number 1 is displayed.
b. Now the operator x++ increases the value of x by 1, so that the value of x becomes 2. The conditional Boolean expression x<=10 is verified.
c. Since this condition is true, the output thread returns and the value of x is printed on the console, i.e. 2.
d. Then the value of x increases again and becomes 3. Since 3 is also less than 10, the thread goes back and outputs the value of x.
e. This process continues until the value of x is less than or equal to 6.
f. Since the value of x is greater than 6, the condition is false and the loop is interrupted.
2. Here is another example of a program based on the do while statement, in which we display a multiplication table.
Program source code 2 :
package javaProgram;public class Test {public static void main(String[] args){int row, column;System.out.println(Multiplication table n);row = 1; // Initialize.do {column = 1;do{int x = row * column;System.out.printf(%4d, + x);column = column + 1;}while(column <= 5);System.out.println(n);row = row + 1;} while(row <= 5);}
Exit:
Table of multiplication:
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
In this example program, the do-while loop has a nested form and produces the following result.
Program source code 3 :
package javaProgram;
import java.util.Scanner;
public class Test {
public static void main(String[] args)
{
int num = 0, sum = 0 ;
// Create an object of the Scanner class to receive the input data.
Scanner sc = new Scanner(System.in) ;
// Continue reading data until the input equals 0.
do{
// Read the next input.
System.out.println(Enter an integer);
num = sc.nextInt();
sum = sum + num;
} while(num != 0);
System.out.println(Sum of numbers: +sum);
}
}
Exit:
Enter a whole number
20
Enter a whole number
10
Enter a whole number
5
Enter a whole number
40
Enter a whole number
0
Sum of numbers : 75
Difference between the while loop and the do-while loop
The difference between while and do-while loops in Java is the following:
1. In the while loop, the test condition is checked first, and then the statements in the while block are executed.
In the do-while loop, the operators in the do block are executed first, and then the test condition is checked.
2. If the test condition is wrong the first time, the instruction block is executed zero times.
The first time the test condition is false, the instruction block is executed at least once.
3. In the while syntax, the while statement is at the top.
In the do-while syntax, the while statement is at the end.
I hope this tutorial covers almost all important points regarding the do while loop in Java with some example programs. I hope you understand this simple subject.
In the next lesson, we will learn about the for loop in Java using an example program.
Thanks for reading!!!
This source has been very much helpful in doing our research. Read more about java do while loop with user input and let us know what you think.
Frequently Asked Questions
Do While loop in Java examples?
The while loop is a flow control structure that allows you to execute a block of code while a condition is true. The structure of this loop begins with the while keyword, followed by a condition or expression. The loop continues as long as this condition is true; otherwise, control passes to the code immediately following the loop. The do while loop is identical to the while loop, except that the condition is tested at the end of the loop. The do while loop is a looping control structure that performs a set of instructions, which may include statement blocks. Basically, the do while loop will execute at least one time, as long as the specified condition is true. In other words, the do while loop will execute at least one time, unless the specified condition evaluates to be false.
How do you write a while loop in Java?
While loops are used in Java programming to repeat a task indefinitely until a certain condition is met. The idea is to use the while loop in situations where you want to wait for a condition to be completed before you run your code. The while loop is very similar to the do-while loop. The while loop has two parts, the condition and the body. The condition is tested before the body of the while loop is executed, while the do-while loop has the condition at the end. In a previous post you saw how to use a while loop to keep running a piece of code until a certain condition is true. There are many different types of loops in Java, and one of the most useful is the do while loop. Unlike a while loop, a do while loop will always run at least once, even if the condition is not true. (The while loop will only run once if the condition is not true)
How do while loops work in Java?
The Java programming language is object oriented, meaning that the program is treated as a set of objects that interact with each other. One of the most popular ways of programming object oriented code is through the use of while loops. While loops are used to repeat a certain block of code a certain number of times. A while loop allows you to keep repeating a chunk of code until a certain condition is fulfilled. The structure of a while loop is as follows: The code within the while loop is called the loop body. It is executed repeatedly until the condition becomes false. The condition in the while loop is called the loop test , and is normally written as a boolean expression. The loop test is usually placed right after the while keyword, and it is normally followed by a statement to be executed each time the loop is run. The until keyword is used to create a while loop with the inverse logic: it runs its body if and only if the loop test evaluates to false.
Related Tags:
while loop example javawhile loop practice problems javawhile loop java multiple conditionsjava do while loop with user inputfor loop in javado while loop example,People also search for,Feedback,Privacy settings,How Search works,Do while loop,While loop,For loop,Infinite loop,See more,while loop example java,while loop practice problems java,while loop java multiple conditions,java do while loop with user input,for loop in java,do while loop example,do-while loop with switch case in java,do while loop example c++