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 trial

Java Java Basics Perfecting the Prototype Looping until the value passes

Aldrich Alviar
Aldrich Alviar
2,745 Points

do while loops

do while loops

Example.java
// I have initialized a java.io.Console for you. It is in a variable named console.
String response;
response = console.readLine("Do you understand do while loops?");
do {
  response = console.readLine("Do you understand do while loops?");
  if (responseIsNo) {
    console.printf("The loop should continue running as long as the response is no. Try again. \n\n");
  }
} while();

1 Answer

So, just a hint: If you post a question to the community, make sure you actually ask a question :)

Now, I'm assuming you're wondering why your code isn't working. I'll explain it to you.

A do-while loop works like this: You tell the code to do something at least once. Then you want it to loop through the code over and over again while a certain condition is met. Here's an example:

String loop;
do{
   loop = console.readLine("Do you want to loop again?");
} while (loop.equals("yes"));

So what this is saying is you have a String called loop. The console asks if you want to continue. As long as you keep saying yes, it will keep looping through. As soon as you say something other than "yes", it will break out of the loop.

Now you can apply this to the example! Let me know if you have any other questions :)