Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialGary Curkan
497 PointsI don't understand do-while loops
String response;
do {
response = ("Do you understand do while loops?");
}
I don't understand how to make the response "no," so the program can keep running; or how to make it yes to stop it. How do I continually prompt the user? I could print something out that says "try again," but that wouldn't just make the computer decide to just print it out again.
This is challenge task 2 of 3 for Java Basics
1 Answer
Kevin Faust
15,353 PointsFirst, remember we are taking input from user so it's
response = console.readLine("Do you understand do while loops?");
And secondly, add this:
while (response == "No");
So the "do" block of code runs for the first time. We take input from user. While the response is equal to "No", then, the loop will run again and again and again until the response variable is equal to anything other than "No"
String response;
do {
response = console.readLine("Do you understand do while loops?");
} while (response.equals("No"));
So as you can see above, the "do" block will keep on running as long as the while statement is true
Gary Curkan
497 PointsGary Curkan
497 PointsThanks for the response! That makes a lot more sense! What makes the statement true though? Because I typed "no"?
Kevin Faust
15,353 PointsKevin Faust
15,353 PointsYup. the statement is true when you type "No". not "no". Java is case sensitive so the two are not the same.
But let's say we type in "No"
Then our while loop will look like this:
("No" == "No")
Since they are equal to each other, the statement is true and the loop ends.