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 trialAsad Sajon
Courses Plus Student 918 PointsDon't understand do...while loop
Hi,i'm feeling pained.Don't understand the 'do..while' loop.I have learned 'While' and 'For' loop with so much interest.But don't understand the 'do...while' loop.Really feeling disappointed.Anyone can help me?
3 Answers
Samuel Webb
25,370 PointsWith a while loop, it could potentially never run. If the condition is false, you will never run the code inside.
while (false) {
console.log("I will not run since my condition is false.");
}
The only difference with a do...while loop is that no matter what, it will run the first time. The reason for this is that, unlike the while loop, it doesn't evaluate the condition until after the first iteration of the loop has run.
do {
console.log("I'll run once even though my condition is false;");
} while (false);
This is the only difference between a while and a do...while loop. If you understand a while loop, you pretty much understand a do...while loop.
Hopefully this clears things up for you.
Samuel Webb
25,370 PointsNo problem. Glad to help.
Asad Sajon
Courses Plus Student 918 PointsThanks a lot Samuel