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

Daniel Bomban
Daniel Bomban
1,926 Points

Feel like I didn't understand do while loops :(

Can someone help me to understand It abit more?

Example.java
// I have initialized a java.io.Console for you. It is in a variable named console.
String response;
boolean doUnderstand;

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

  if (doUnderstand) {
    console.printf("Sorry, practice more! :)");
  }
} while(true);

3 Answers

Daniel Heyde
Daniel Heyde
3,352 Points

A while loop is structured like this:

while(condition){ 
run code 
}

This means that whatever is inside the loop will run as long as the condition is true. A do while works the same way, except the 'do' part ensures that it runs once beforehand. The 'do' part runs your code once, and THEN checks if the condition is true. After the 'do' part runs once, it works like a normal while loop.

Keith Dowd
Keith Dowd
4,404 Points

The do-while loop works by executing the code block between the curly braces once and then evaluates the while condition to determine if the code block between the curly braces should be executed again. If the condition evaluates to true then the code block is executed again. The code block continues to be executed again and again until the while condition evaluates to false.

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Do you feel like you get it now @Daniel Bomban ? Also for more practice with loops I made a live workshop called Feeling Loopy with Java.

Make sure to mark the "best answer" if they helped you out.

Hope it's clearing up for you!

Daniel Bomban
Daniel Bomban
1,926 Points

Going to take the Feeling Loopy course ASAP for better understanding in this subject,

Thank you :)