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

JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops `do ... while` Loops

Why use use a "do" and write more code, when you could just use the while loop by itself and accomplish exact same thing

I can just use the while loop by itself to pull up a prompt over and over till the user gives the right answer. So why use the "do"? I still don't understand what the difference is? I see that we use an if conditional statement instead. And boolean. But it does the same thing. Is it just to show us another way? It was a little confusing because he gives the "prompt" loop thing as a scenario where you WOULD use the do/while, but that can be done with just the while loop. They made us do just that in the review/challenge right before this video

3 Answers

Robert Stefanic
Robert Stefanic
35,170 Points

I have a couple of friends in school/grad programs for CS who I've asked about this. One said, "Never use a do/while loop. It's bad practice. I've had professors say that bottom driven loops are inefficient." And I've had others say, "I never use them, but I could see why someone would use them."

The one who said that the do/while is "bad practice" is into system programming and assembly, and the one who said that they aren't bad uses higher level languages. For JavaScript, I don't think it matters. If you want to just write a normal while loop that goes through it at least once, then set it up that way. Whatever way works for you. Just think of it as an extra tool in your tool kit that you can use. Don't feel obligated to use them if you don't want to. :)

Erik Nuber
Erik Nuber
20,629 Points

The difference is that a "do...while" loop will always run what is inside of the loop at least one time before the condition is checked. A "while" loop will only run if the condition is true.

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

The main difference between a while loop and a do while loop is that the evaluation comes at the end of the do while loop while the evaluation comes at the beginning of the while. The result, is that the code inside the do while loop is guaranteed to run at least once, while it is not guaranteed to run at all using the while. But, I think you're correct. I think the point is to illustrate the differences and let you know that they exist.

That being said, in my experience, most do while loops can be rewritten as a while loop with proper setup in advance.

Hope this helps! :sparkles: