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

Sean Walker
Sean Walker
1,792 Points

Syntax error in do while loop

Hey everyone,heeeeeeeeeeeeeeeeeeelp me! :)) i'm really confused about this do while loop...pls tell me what to do to fix my code...thanks!

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?  ");
do {
  if (response.equals("no")) 
} while String response = console.readLine("do you understand do while loops?  "); 

1 Answer

There is a way in which do-while loop works(similar for while loop).

There are 2 things to note:

1:> DO{ do some tasks; }WHILE (till this condition satisfies);

2:> Every sentence in java ends with a semi-colon(;)

Now lets see the code:

//Lets create a variable called response
String response ;

//Now lets ask for response and if response in "No", we continue asking;
do {
  //asking for response from user
  response = console.readLine("do you understand do while loops?  ");
} while (response.equals("No"));
//after while is a semi-colon

Hope this helps.

Also, it is worth noting that first "do" block runs and then check for condition. If condition satisfies then it run again otherwise it stops.

So unlike "for" and "while" loop, "do-while" always runs at least once.