Javatpoint Logo
Javatpoint Logo

Difference Between While and Do While loop in Java

Loops play a crucial role in programming as they allow developers to execute a block of code repeatedly until a certain condition is met. In Java, there are several types of loops, with two commonly used ones being the "while" loop and the "do-while" loop. While these two loops might seem similar at first glance, they have some key differences that can impact the flow of your program. In this section, we will explore the dissimilarities between while and do-while loops in Java and when to use each one.

The While Loop

The while loop is a pre-test loop, meaning that it evaluates the condition before entering the loop body. If the condition is true, the loop body is executed. If the condition is false from the start, the loop body will not be executed at all. Here's the general syntax of a while loop in Java:

The loop will continue to execute the code block repeatedly as long as the condition remains true. If the condition becomes false at any point, the loop terminates, and the program continues with the next statement following the loop.

Example of While Loop:

In this example, the loop will print the numbers from 1 to 5. The condition i <= 5 is checked before each iteration, and as long as it evaluates to true, the loop will keep executing.

The Do-While Loop

The do-while loop is a post-test loop, which means it executes the loop body at least once before checking the condition. This ensures that the loop body is executed at least once, regardless of whether the condition is true or false. The syntax of a do-while loop in Java is as follows:

The loop will repeatedly execute the code block while the condition remains true. It will stop only when the condition becomes false.

Example of Do-While Loop

In this example, the loop will also print the numbers from 1 to 5. However, the key difference here is that the loop body is executed first before checking the condition j <= 5.

Java While Vs. Do-While Loops

S.N. While Do-while
1 Condition is at top. Condition is at the bottom.
2 Braces are not required if there is single statement in the body. Braces are required if there is single statement in the body.
3 There is no semicolon at the end of while. Semicolon is necessary at the end of do-while loop.
4 Computer executes the body if and only if condition is true. Computer executes the body at least once even if condition is false.
5 It should be used when condition is more important. This should be used when the process is important.
6 It is also known as entry controlled loop. It is also known as exit controlled loop.
7 while(n<5)
{
printf("%d\n", n);
}
do
{
printf("%d\n", n);
}
while(n<=5);

Condition Evaluation

While loop: The condition is checked before each iteration. If the condition is initially false, the loop body will not be executed at all.

Do-while loop: The condition is checked after each iteration. The loop body is executed at least once, even if the condition is false from the start.

Execution

While loop: Executes zero or more times (0 if the condition is false initially).

Do-while loop: Executes one or more times (at least once).

Use Cases

While loop: Use when you want to repeat a block of code based on a condition, but the code should not run if the condition is false initially.

Do-while loop: Use when we need to execute a block of code at least once, irrespective of the condition, and then repeat it based on the condition.

Which Loop to Choose?

The choice between the while loop and the do-while loop depends on the specific requirements of your program. If we want to ensure that the loop body is executed at least once, then the do-while loop is more appropriate. On the other hand, if the loop may not need to run at all, based on an initial condition check, then the while loop is a better choice.

Both loops are valuable tools in a Java developer's arsenal and offer flexibility for handling different situations in code.

The while loop and the do-while loop in Java share similarities in functionality, but their behavior regarding condition evaluation and initial execution makes them distinct. Understanding these differences is essential for writing efficient and error-free code. Choose the loop that best suits the specific requirements of your program, and we will be well-equipped to handle repetitive tasks effectively.

While Loop Example

WhileLoopExample.java

Output:

Enter a number: 5
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5

Do-While Loop Example

DoWhileLoopExample.java

Output:

Enter a number: 5
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA