Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Do-While Loop | Loops
Java Basics

Do-While LoopDo-While Loop

What is the difference between while and do-while?

The do-while loop is another type of loop in Java that is similar to the while loop. However, it has one important difference: the condition is checked at the end of each iteration. This means the code block will always execute at least once before evaluating the condition.

Here's the basic syntax of the do-while loop:

java

Main.java

Here are some key points to keep in mind about the do-while loop:

  • Execution Flow: The code block is executed first, and then the condition is checked. If the condition is true, the loop continues to the next iteration. If the condition is false, the loop terminates, and the program continues with the next statement after the loop;
  • Guaranteed Execution: Since the code block is executed before the condition check, the do-while loop is useful when you want to ensure that the code block runs at least once, regardless of the initial condition;
  • Variable Scope: Variables defined within the code block of a do-while loop have a scope limited to that code block. They cannot be accessed outside the loop;
  • Use Cases: The do-while loop is commonly used when you want to prompt the user for input at least once and then continue the loop based on a condition. It is also useful when iterating through a list of elements, ensuring that the loop runs at least once, even if it is empty.

Let's look at a simple example of usage and compare the results of a while loop and a do-while loop on a very basic example:

java

main.java

while loop:

java

main.java

We can see that when executing the do-while loop with a condition that is always false, we executed the body of the loop once, while the while loop simply gave us an error. This is the only and most fundamental difference between these two loops.

By the way, the image above perfectly illustrates the difference.

Note

Remember to ensure that there is a condition in place to eventually terminate the do-while loop to prevent infinite looping.

1. What will be the output of the code?
2. What will be the output of the code?

What will be the output of the code?

Select the correct answer

What will be the output of the code?

Select the correct answer

Everything was clear?

Section 3. Chapter 3
course content

Course Content

Java Basics

Do-While LoopDo-While Loop

What is the difference between while and do-while?

The do-while loop is another type of loop in Java that is similar to the while loop. However, it has one important difference: the condition is checked at the end of each iteration. This means the code block will always execute at least once before evaluating the condition.

Here's the basic syntax of the do-while loop:

java

Main.java

Here are some key points to keep in mind about the do-while loop:

  • Execution Flow: The code block is executed first, and then the condition is checked. If the condition is true, the loop continues to the next iteration. If the condition is false, the loop terminates, and the program continues with the next statement after the loop;
  • Guaranteed Execution: Since the code block is executed before the condition check, the do-while loop is useful when you want to ensure that the code block runs at least once, regardless of the initial condition;
  • Variable Scope: Variables defined within the code block of a do-while loop have a scope limited to that code block. They cannot be accessed outside the loop;
  • Use Cases: The do-while loop is commonly used when you want to prompt the user for input at least once and then continue the loop based on a condition. It is also useful when iterating through a list of elements, ensuring that the loop runs at least once, even if it is empty.

Let's look at a simple example of usage and compare the results of a while loop and a do-while loop on a very basic example:

java

main.java

while loop:

java

main.java

We can see that when executing the do-while loop with a condition that is always false, we executed the body of the loop once, while the while loop simply gave us an error. This is the only and most fundamental difference between these two loops.

By the way, the image above perfectly illustrates the difference.

Note

Remember to ensure that there is a condition in place to eventually terminate the do-while loop to prevent infinite looping.

1. What will be the output of the code?
2. What will be the output of the code?

What will be the output of the code?

Select the correct answer

What will be the output of the code?

Select the correct answer

Everything was clear?

Section 3. Chapter 3
some-alt