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
🌐
Stack Overflow
stackoverflow.com › questions › 48492214 › while-loops-java
While Loops Java - Stack Overflow
You are a genius!"); correctQuestions++; overallQuestions++; } else System.out.print("Sorry, looks like you'll have to try again"); while(number1 * number2 == answer ^ number1 * number2 != answer) System.out.print("Would you like to try another question ?
🌐
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
Discussions

do while loop - Java Basic
Tanguy Muffat is having issues with: Hi guys, I would need you support. I'm stucked under challenge 2/3 where I'm being asked: "Now continually prompt the user in a do whil... More on teamtreehouse.com
🌐 teamtreehouse.com
2
November 20, 2017
Is it me, or is java for loops and while loops pretty much the same thing(Java)
You're right in that there are use cases for which either one can work. For loops are best when you're looping for a defined number of times (once per array element, for instance). While loops can simulate for loops perfectly well, but are best used when the number of loops isn't well-defined -- for instance, loop until the user types the word "END", or perhaps loop indefinitely). In your example, the benefit of a for loop over a while loop is that the loop conditions are more readable, and that's often a very compelling reason to choose one method over another. More on reddit.com
🌐 r/learnprogramming
6
2
April 18, 2021
Need help with Java while loops
Guys, I need help with my java code, I have been working on this problem for hours. The problem is that when my inner while loop turns out true, it returns to the outer while loop, and the inner while loop doesn’t run anymore. Is there another alternative for this method because I want the ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
1
0
June 11, 2021
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
March 29, 2016
People also ask

How do you exit a 'while loop' in Java?
To exit a 'while loop' in Java, you can use the 'break' statement, which immediately terminates the loop and transfers control to the code following the loop. Alternatively, you can update the loop's condition to become false, allowing the loop to exit naturally on the next iteration check.
🌐
vaia.com
vaia.com › java while loop
Java While Loop: Examples & Iterations | Vaia
What is the difference between a 'while loop' and a 'do-while loop' in Java?
A 'while loop' checks its condition before executing any statements, meaning it may not execute at all if the condition is false initially. In contrast, a 'do-while loop' executes the statements once before checking the condition, ensuring that the loop executes at least once regardless of the initial condition.
🌐
vaia.com
vaia.com › java while loop
Java While Loop: Examples & Iterations | Vaia
How do you create an infinite 'while loop' in Java?
An infinite 'while loop' in Java can be created by using the syntax `while(true) { // code }`, where the condition `true` ensures the loop continues indefinitely until it is forcibly stopped or an exit condition is implemented using a `break` statement.
🌐
vaia.com
vaia.com › java while loop
Java While Loop: Examples & Iterations | Vaia
🌐
BeginnersBook
beginnersbook.com › 2015 › 03 › while-loop-in-java-with-examples
While loop in Java with examples
Fourth iteration: value of i is 3, fourth element of the array represented by arr[3] is printed. After fourth iteration: value of i is 4, the condition i<4 returns false so the loop ends and the code inside body of while loop doesn’t execute. Practice the following java programs related to while loop:
🌐
DEV Community
dev.to › satyam_gupta_0d1ff2152dcc › master-the-java-while-loop-a-beginners-guide-with-examples-best-practices-1371
Master the Java While Loop: A Beginner's Guide with Examples & Best Practices - DEV Community
October 13, 2025 - As a beginner in programming, one of the first mind-blowing concepts you encounter is the ability to make a computer do the same task over and over again, without complaining. It’s the very essence of what makes computers powerful. In Java, one of the most fundamental tools for achieving this repetition is the humble while loop.
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.
Find elsewhere
🌐
DataFlair
data-flair.training › blogs › java-while-loop
Java While Loop with Examples - DataFlair
April 10, 2024 - Once the condition becomes false, the loop terminates, and the control flow breaks out of the while loop. The key purpose of the Java while loop is to repeat a set of statements as long as the test condition holds true. The while loop is ideally used when the number of iterations is not predetermined.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-while-loop-with-examples
Java while Loop - GeeksforGeeks
Java while loop is a control flow statement used to execute the block of statements repeatedly until the given condition evaluates to false.
Published   2 weeks ago
🌐
Vaia
vaia.com › java while loop
Java While Loop: Examples & Iterations | Vaia
November 14, 2023 - A Java while loop repeatedly executes a block of code as long as a given condition evaluates to true, making it ideal for situations where the number of iterations isn't predetermined. This loop syntax starts with the "while" keyword, followed by the condition within parentheses and the code ...
🌐
CodeGym
codegym.cc › java blog › loops in java › java while loop
Java While Loop
April 2, 2025 - If you imagine a program that captures your attempts to recite a poem, it would also have to use a loop: While (success < 3) learn a poem Similar constructions are used in all modern educational apps that are used, for example, in learning languages or playing musical instruments. To solve problems like the examples above, and more generally, to automate repetitive actions in Java, you can use the while loop.
🌐
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 ...
🌐
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
🌐
Hyperskill
hyperskill.org › university › java › java-while-loop
Java While Loop
November 21, 2024 - while (condition) { // body: do something repetitive } A loop body can contain any correct Java statements, including conditional statements and even other loops, the latter being called nested loops.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-do-while-loop-with-examples
Java Do While Loop - GeeksforGeeks
The loop continues until the condition c <= 5 becomes false, after which the loop terminates. ... Note: The test_expression in a do-while loop must evaluate to a boolean value; otherwise, a compile-time error occurs.
Published   2 weeks ago
🌐
IDC Online
idc-online.com › technical_references › pdfs › information_technology › While_Loops_in_Java.pdf pdf
WHILE LOOPS IN JAVA Description
In Java there are three types of loops:  A while loop, in which the loop-controlling Boolean expression is the first · statement in the loop ·  A for loop, which is usually used as a concise format in which to execute loops ·  A do...while loop, in which the loop-controlling Boolean expression is the last ·
🌐
Reddit
reddit.com › r/learnprogramming › is it me, or is java for loops and while loops pretty much the same thing(java)
r/learnprogramming on Reddit: Is it me, or is java for loops and while loops pretty much the same thing(Java)
April 18, 2021 -

So i am in the middle of learning Java, and today i worked on loops. Specifically for and while loops. However, when i was given this problem to solve on codeacdemy, i noticed this.

While Loop

Examples 2:

class Coffee {

public static void main(String[] args) {

// initialize cupsOfCoffee

int cupsOfCoffee = 1;

// add while loop with counter

while(cupsOfCoffee <= 100) {

System.out.println("Fry drinks cup of coffee #" +cupsOfCoffee );

cupsOfCoffee ++;

}

}

}

Compared to:

For Loops:

class Coffee {

public static void main(String[] args) {

for (int cupsOfCoffee = 1;cupsOfCoffee <= 100;cupsOfCoffee++) {

System.out.println("Fry drinks cup of coffee #" + cupsOfCoffee);

}

}

}

For some reason, even though they pretty much resulted in the same output, for loops and while loops do pretty much the same thing, but for loops feel a tiny bit more simple(imo).

🌐
Baeldung
baeldung.com › home › java › core java › java while loop
Java While Loop | Baeldung
February 16, 2025 - It repeats a statement or a block of statements while its controlling Boolean-expression is true. ... The loop’s Boolean-expression is evaluated before the first iteration of the loop – which means that if the condition is evaluated to false, the loop might not run even once. ... In this quick tutorial, we explored Java’s while loop.
🌐
Reddit
reddit.com › r/learnprogramming › java: when to use a while loop?
r/learnprogramming on Reddit: Java: When to use a While Loop?
March 29, 2016 -

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.
🌐
Udemy
blog.udemy.com › home › java while loops, do-while loops, and infinite loops
Java While Loops, Do-While Loops, and Infinite Loops - Udemy Blog
December 4, 2019 - There are three kinds of loop statements in Java, each with their own benefits – the while loop, the do-while loop, and the for loop.
🌐
ScholarHat
scholarhat.com › home
Understanding while loop in java (With Syntax and Example)
September 6, 2025 - Ternary Operator in Java with Examples: Ternary Operator vs. if...else Statement ... while loop in Java is a control flow statement that repeatedly executes a block of code as long as a specified Boolean condition is true.