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

do while loop

i have to prompt the user in a do while loop. The loop should continue running as long as the response is No. And I don't have to forget to declare response outside of the do while loop. thx in advance :)

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 while loops? ");
do {
  isInvalidWord = (response.equalsIgnoreCase("no"));
}
String response;

1 Answer

Hey Ramy,

First, you only need to declare the String response once, at the top of your program. Inside of your do-while loop is where you'll want to initialize the variable with your question to the user. This way, the user will be continously prompted with the question, "do you understand while loops?" until the while condition is false. With that said, a do-while loop needs a while condition. Inside of the parenthesis is where you'll want to test the user's response.

Take a look:

// Variable to store user input
String response;
do {
    // Prompt the user for input
   response = console.readLine("do you understand while loops? ");
   // Continue to run loop while response is "no" while ignoring case
} while (response.equalsIgnoreCase("no"));