Hi Tanguy, I have seen this problem come up a lot. You do not actually need a boolean to run your do while loop, and you can simplify it a lot by having the loop depend on the response directly. java // I have initialized a java.io.Console for you. It is in a variable named console. String response; do{ response = console.readLine("Do you understand do while loops?"); }while(response.equalsIgnoreCase("no")); You may also include your print statement that you included using if(response.equalsIgnoreCase("no")). HOWEVER, with all that being said, what you did actually works fine! You just made a typo in your code. You wrote invalidReponse not invalidReSponse, as declared outside your loop. You can see these errors easily if you check the compiler error (You would see a symbol not found error point to that variable) Hope that helps! Answer from Patrick Bluth on teamtreehouse.com
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_while_loop_do.asp
Java Do/While Loop
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... The do/while loop is a variant of the while loop. This loop will execute ...
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ tutorial โ€บ java โ€บ nutsandbolts โ€บ while.html
The while and do-while Statements (The Javaโ„ข Tutorials > Learning the Java Language > Language Basics)
The difference between do-while and while is that do-while evaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within the do block are always executed at least once, as shown in the following DoWhileDemo program:
Discussions

do while loop - Java Basic
The loop should continue running as long as the response is No. Don't forget to declare response outside of the do while loop." I entered the following but I iget a compiler error. Any Idea where I do wrong. Thank you for your support. Tanguy ... // I have initialized a java.io.Console for you. More on teamtreehouse.com
๐ŸŒ teamtreehouse.com
2
November 20, 2017
Java: When to use a While Loop?
Imagine you have a collection of 1,000,000 songs. You want to search for one song specifically, and return that song (or, if the song doesn't exist in the collection, tell you it wasn't found). A for or for-each loop could accomplish that, but it's going to loop 1,000,000 times even if the song is the very first record checked, which is a huge waste of time and power. If you write a while loop and then have it stop when a matching record is found, it runs exactly as much as is necessary and no more. Another example -- imagine a simple text-based console app that reads input from a user and then prints it out on the screen, but then closes when they type "quit". If you put the steps of your program - asking the user for input, assigning it to a variable, and then printing it out - in a for/for-each loop, how do you know how many times to run it? A while loop that stops running when the user types "quit" lets the user flexibly quit whenever they want, whether on 0 entries or 200. There's a lot of other applications but hopefully these are useful to conceptualize at least. More on reddit.com
๐ŸŒ r/learnprogramming
19
13
November 21, 2015
java - do-while and while comparison - Stack Overflow
I am learning about do-while vs ... above java fragment (already declared and initialized) using a while instead. Are the below rewritten codes correct way to do so: ... The difference is when the condition check occurs, of course. In a do-while, the body is always executed at least once. Otherwise, there really is nothing to say. ... while is an entry check loop, whereas do-while ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
A very simple java do...while loop - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... Closed 10 years ago. I am learning JAVA and typed the following DO...WHILE example. More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-do-while-loop-with-examples
Java Do While Loop - GeeksforGeeks
The do-while loop executes the do block at least once, even though the condition c >= 3 is false. Since there is only one statement inside the loop, it runs once and then terminates when the condition fails.
Published ย  1 week ago
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ java โ€บ java_do_while_loop.htm
Java - do...while Loop
The do-while loop is an exit control loop where the condition is checked after executing the loop's body. ... Notice that the Boolean expression appears at the end of the loop, so the statements in the loop execute once before the Boolean is tested.
๐ŸŒ
Programiz
programiz.com โ€บ java-programming โ€บ do-while-loop
Java while and do...while Loop
If the textExpression evaluates to true, the body of the loop inside the do statement is executed again. This process continues until the textExpression evaluates to false. Then the loop stops. ... Let's see the working of do...while loop. // Java Program to display numbers from 1 to 5 import java.util.Scanner; // Program to find the sum of natural numbers from 1 to 100.
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_while_loop.asp
Java While Loop
In the next chapter, you will learn about the do while loop, which always runs the code at least once before checking the condition. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com ยท If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com ยท HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ java-do-while-loop
Java do while loop | DigitalOcean
August 3, 2022 - Java do-while loop is used to execute a block of statements continuously until the given condition is true. The do-while loop in Java is similar to while loop except that the condition is checked after the statements are executed, so do while loop guarantees the loop execution at least once.
Find elsewhere
Top answer
1 of 2
3
Hi Tanguy, I have seen this problem come up a lot. You do not actually need a boolean to run your do while loop, and you can simplify it a lot by having the loop depend on the response directly. java // I have initialized a java.io.Console for you. It is in a variable named console. String response; do{ response = console.readLine("Do you understand do while loops?"); }while(response.equalsIgnoreCase("no")); You may also include your print statement that you included using if(response.equalsIgnoreCase("no")). HOWEVER, with all that being said, what you did actually works fine! You just made a typo in your code. You wrote invalidReponse not invalidReSponse, as declared outside your loop. You can see these errors easily if you check the compiler error (You would see a symbol not found error point to that variable) Hope that helps!
2 of 2
0
I think i'm very unfamiliar with this kind of Java syntax. Using the console.printf() instead of System.out.println() and console.readLine() instead of scanner.nextLine(). I still don't understand why Tanguy Muffat's program works after fixing the syntax error. I don't understand how having the [while(response.equalsIgnoreCase("no"));] at the bottom works. Normally i would expect it to look more like : do{ while(response.equalsIgnoreCase("no")) { console.printf("Sorry you cannot continue the training. Train harder to understand the concept. \n"); }} I would think that a while condition with no brackets under it and nothing inside of it wouldn't work. In Tanguy Muffat's program it looks like the while loop is completely outside of the do loop.
๐ŸŒ
ScholarHat
scholarhat.com โ€บ home
do...while Loop in Java - Flowchart & Syntax (With Examples)
September 6, 2025 - Read More: Top 50 Java Interview Questions and Answers ยท The do...while loop in Java is also known as Exit Control Loop. It is used byJava Developerswhen they want a block of code to be executed at least once before checking the condition.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ java: when to use a while loop?
r/learnprogramming on Reddit: Java: When to use a While Loop?
November 21, 2015 -

My Intro to programming class has covered how to use both for and while loops. As for the code I am good to go with it but I am having a little trouble understanding them.

When would I use a while loop? Why would that be the better choice for the loop over a for loop?

Though I know how to code them I do not quite have a full comprehension on determining which to use and why one would be better than the other.

Any examples and/or like laymen's break down would be much appreciated!

Top answer
1 of 5
11
Imagine you have a collection of 1,000,000 songs. You want to search for one song specifically, and return that song (or, if the song doesn't exist in the collection, tell you it wasn't found). A for or for-each loop could accomplish that, but it's going to loop 1,000,000 times even if the song is the very first record checked, which is a huge waste of time and power. If you write a while loop and then have it stop when a matching record is found, it runs exactly as much as is necessary and no more. Another example -- imagine a simple text-based console app that reads input from a user and then prints it out on the screen, but then closes when they type "quit". If you put the steps of your program - asking the user for input, assigning it to a variable, and then printing it out - in a for/for-each loop, how do you know how many times to run it? A while loop that stops running when the user types "quit" lets the user flexibly quit whenever they want, whether on 0 entries or 200. There's a lot of other applications but hopefully these are useful to conceptualize at least.
2 of 5
10
There's actually 4 types of loops in Java. A normal for loop (for(int i = 0; etc), a while loop, a do-while loop and a for-each (or enhanced for loop). Examples: For loop: for(int i = 0;i < someNumber;i++) { //Do something } While loop: while(expression) { //Do something } Do-while: do { //Do something } while(expression); Enhanced for: for(Element e : collectionOfElements) { //Do something with E. } In essence they all do the exact same thing: repeat the inner block based on a condition. When and how the condition is checked differs between them and you use each depending on what you need to do. A very rough guideline is: For: when you know beforehand you have to do something N number of times For-each: when you need to iterate over a collection, iterable or array While: when you need to loop an unknown number of times and need to check the condition to continue before the code block. Do-while: when you need to loop an unknown number of times and need to check the condition after the code block. Also keep in mind that break (break out of the loop) and continue (skip the rest of the code and go directly to the next iteration) are all control statements that work for all loops. So it's very possible to use a while as a for loop or vice versa.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ loops-in-java
Java Loops - GeeksforGeeks
The do-while loop ensures that the code block executes at least once before checking the condition. Example: The below Java program demonstrates a do-while loop that prints numbers from 0 to 10 in a single line.
Published ย  August 10, 2025
๐ŸŒ
Javatpoint
javatpoint.com โ€บ java-do-while-loop
Java Do While Loop - javatpoint
January 10, 2022 - Java do while loop, java do while loop examples, nested do while loop in java, difference between java while loop and do while loop with concepts and examples.
๐ŸŒ
Upgrad
upgrad.com โ€บ home โ€บ blog โ€บ software development โ€บ do while loop in java: syntax, examples, and practical applications
Do-While Loop in Java: Examples, Applications & Tips 2025
September 10, 2025 - The counter num keeps increasing by 1 until it reaches the value 6. Final Condition Check: Once num = 6, the condition 6 <= 5 is checked and is false. Since the condition is false, the do while loop Java stops executing.
๐ŸŒ
Coding Shuttle
codingshuttle.com โ€บ java-programming-handbook โ€บ java-while-and-do-while-loop
Java while and do while loop | Coding Shuttle
April 9, 2025 - In the while loop, the condition ... In the do-while loop, the condition is checked after the loop has executed, ensuring that the loop body runs at least once, even if the condition is false initially....
๐ŸŒ
ScholarHat
scholarhat.com โ€บ home
Looping Statements in Java - For, While, Do-While Loop in ...
It iterates over the values of i (1 and 2) and j (1 to 10), outputting the result of i * j after each iteration, using two nested do...while loops. A nested while loop and for loop in Java involves placing a while loop inside a for loop or vice versa. For each iteration of the outer loop (either while or for), the inner loop will run completely.
Published ย  August 30, 2025
Top answer
1 of 4
20

The difference between a do-while and a while is when the comparison is done. With a do-while, you'll compare at the end and hence do at least one iteration.

Equivalent code for your example

do
{ 
    i++; 
    ++j;
    System.out.println( i * j );

}
while ((i < 10) && (j*j != 25));

is equivalent to:

i++; 
++j;
System.out.println( i * j );
while ((i < 10) && (j*j != 25)) {
    i++; 
    ++j;
    System.out.println( i * j );
}

General comprehension

A do-while loop is an exit controlled loop which means that it exits at the end. A while loop is an entry controlled loop which means that the condition is tested at the beginning and as a consequence, the code inside the loop might not even be executed.

do {
    <block>
} while (<condition>);

is equivalent to:

<block>
while (<condition>) {
    <block>
};

Use case

A typical use case for a do-while is the following: you ask the user something and you want do repeat the operation while the input is not correct.

do {
   // Ask something
} while (input is not correct);

In that case, you want to ask at least once and it's usually more elegant than using a while which would require either to duplicate code, or to add an extra condition or setting an arbitrary value to force entering the loop the first time.

At the opposite, while loops are much more commons and can easily replace a do-while (not all languages have both loops).

2 of 4
3

The key difference between do-while and while, with do-while you are guaranteed at least one run of your code before the checks.

*It does not need to get anymore complicated than that.

๐ŸŒ
IONOS
ionos.com โ€บ digital guide โ€บ websites โ€บ web development โ€บ java do-while loop
How to use the Java do-while loop - IONOS
January 3, 2025 - The do-while loop is used in Java to execute and repeat an instruction until a defined termination condition is fulfilled (i.e., true). Similar loops exist in most programming languages and are used to execute certain blocks of code multiple times.
๐ŸŒ
SitePoint
sitepoint.com โ€บ blog โ€บ java โ€บ javaโ€™s while and do-while loops in five minutes
Java's While and Do-While Loops in Five Minutes โ€” SitePoint
November 5, 2024 - A while loop first checks the condition before executing the loop body. If the condition is false at the start, the loop body will not execute even once. On the other hand, a do-while loop executes the loop body first and then checks the condition.
๐ŸŒ
DevQA
devqa.io โ€บ java-do-and-do-while-statements
Java While and Do-While Loops with Code Examples
January 2, 2023 - Letโ€™s explore a practical example of the do-while loop: import java.util.Scanner; public class DoWhileLoopExample { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int number; do { System.out.print("Enter a number (0 to exit): "); number = scanner.nextInt(); System.out.println("You entered: " + number); } while (number != 0); System.out.println("Loop finished"); } }