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

Neelesh Tewani
Neelesh Tewani
1,239 Points

i am not able to understand the do while loop

String response; do{ response = console.readLine("Do you understand do while loops ?"); if(response.equals("yes")||response.equals("no")){ response = console.readLine("Do you understand do while loops ?"); } }while(response.equals("yes")||response.equals("no")); System.exit(0);

4 Answers

Konrad Pilch
Konrad Pilch
2,435 Points

Do While loop basically means that for example:

From 9am to 5pm , my boss will pay me 100dollars per hour.

So WHILE im in the work from 9am to 5pm, five me ( DO ) 100 dollar each hour. After 5pm , stop giving it and break the loop. Next day from 9am to 5pm, activate WHile Loop.

I hope this helps. If you mean that.

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hey Neelesh,

here is a schematic view of a do/while loop

do{
//here comes your code you want to be executed at least once
//this is also the loop part 
} while(//here comes your condition, it can be true or false, depending on what you are supposed to do, if its true  - the loop will be executed);

The challenge should look like this:

String response; //in this String variable the user input will be stored/assignet 

do { 
response = console.readLine("Do you understand do while loops?"); //the answer of the user will be stored/assigned from console into "response"
  } while(response.equalsIgnoreCase("no")); //while the answer is no or No, the condition is true and the loop goes on, if the condition is false (Yes) the loop stops

I hope it helps ...

Grigorij

Eredis Gutierrez
Eredis Gutierrez
6,531 Points

Hey Neelesh,

A Do while loop does something while the expression is valid.

For example.

int stopwatch = 30;

do {
stopwatch++; //Increments by 1 every time it loops
System.out.println("Stopwatch is at " + stopwatch);
} while (stopwatch < 5) {

}

If you are still having trouble, I recommend for you to look at the Java Docs, They have excellent documentation.

Do While Java Docs

Neelesh Tewani
Neelesh Tewani
1,239 Points

i want help in solving this problem which i have posted before please help me this