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

Jeffrey Kwok
Jeffrey Kwok
551 Points

I'm learning java and I still don't understand the purpose of using a boolean variable while using a do while loop

I'm learning java and I still don't understand the purpose of using a boolean variable while using a do while loop

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

4 Answers

This is the code that the challenge is asking for:

// I have initialized a java.io.Console for you. It is in a variable named console.
String respone; 
do {
  response = console.readLine("Do you understand do while loops? ");
} while(response.equalsIgnoreCase("no"));
console.printf("Because you said %s, you passed the test!", response);  //this will only run if the user types in a response that is NOT: "no", "NO", "No", or "nO".

Simply put, the way a do-while loop works is, DO some code, while the boolean-expression you're testing for is true. So in this particular loop, DO response = console.readLine("Do you understand do while loops? "); IF(while) the user response is "no" then re-execute the code inside the DO code block. The printf statement will only happen when the user types in an answer that is NOT "no".

As far as your code goes, you cannot invoke the readLine method off of a Boolean object variable, because the readLine method saves the user input as a String and NOT a boolean. Your if statement is not needed for this challenge, since your WHILE'S Boolean-expression is already checking if the user input is equal to a certain String.

Also, keep in mind the difference of =, ==, and .equals(). The == operator compares if the objects are the same instance. The equals() operator compares the state of the objects (e.g. if all attributes are equal). When you use the assignment operator (=), you're assigning a value and not testing if it is true or false.

.equalsIgnoreCase() just means that when testing the String, the casing of the String doesn't matter, if you were to just use the .equals method, the Casing would matter. For example:

String response;
do {
  response = console.readLine("Do you understand do while loops? ");
}while(response.equals("no"));  //in this example if the user's input is lowercase "no", then the code inside of the do code block will run again and again until the user types in a different response.

Let me know if all of this makes sense, if not, I can try to re-explain another way.

Do you understand what a while loop does?

The difference between a do-while loop and a while loop is that a do-while loop will execute the block once before checking the condition of the while loop and executing the block again (if the condition evaluates to true, hence the use of a boolean).

Jeffrey Kwok
Jeffrey Kwok
551 Points

thank you for the thorough answer. Appreciate it

No problem

Also, if my answer above was helpful to you, I would really appreciate if you marked as best answer. Thanks! :)