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

not sure what to do for the do while loop

need help with the code

Example.java
// I have initialized a java.io.Console for you. It is in a variable named console.
String response = console.readLine("Do you understand do while loops");

1 Answer

Chase Marchione
Chase Marchione
155,055 Points

Hi Mike,

By using a do-while loop, we are telling the program to do something as many times as it needs to until something specific happens. The do-while loop syntax requires us to use the do keyword first, and then we should put the action that we want to have performed within curly braces. We end the loop with a while statement that includes the condition--that is, what needs to happen in order for our do-while loop to stop executing.

The code for the prompt should be inside of the loop, because that is what needs to be run again and again until the condition to end the loop happens. The reason we keep the variable declaration outside of the loop is so that the compiler doesn't attempt to run a declaration statement for an already declared variable.

Essentially, the idea for this challenge is: We will run (or 'do') the prompt as many times as necessarily, until the user give us a no answer.

String response;

do {
    response = console.readLine("Do you understand do while loops?");
} while (response == "No");

Hope this helps!