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 Create a `do...while` loop

Steven Good
Steven Good
2,303 Points

I'm still a little confused about the benefit of using a 'do ... while' loop over the 'while' loop

In this challenge, where we had to re-write the code to create a 'do while' loop.

I had no major problems changing it, but I still don't really understand why this 'do while' loop is better in this situation. I previewed the original loop and the re-written loop, and they both work exactly the same from a user perspective. In both cases, the loop runs until the user inputs the correct password.

If anyone can explain, I'll be really grateful! Thanks.

5 Answers

Daniel Glennie
Daniel Glennie
42,854 Points

I think it will always depend on the problem you are working on. Each loop will have particular situations where it is the best choice. As I said before with the while and do... while loops it really comes down to whether you want to run the code block that first time before checking the condition or not. For loops are more suited to something like cycling through and operating on each entry in an array, or every second entry etc and they have greater flexibility in terms of what conditions you pass to them. Knowing which loop will work best for a given problem takes time and practice but one should usually consider what kind of problem it is and choose the loop that best performs the task.

Steven Good
Steven Good
2,303 Points

Okay, thanks for clarifying Daniel. I understand it a bit more clearly now.

Daniel Glennie
Daniel Glennie
42,854 Points

I would agree that there appears to be no real improvement in this particular example from changing to the do...while loop but on a more general level the distinction is that do while loops run the code block at least once and then repeat until the condition evaluates to false whereas while loops will run zero times if the conditional is false. Again I fail to see why that presents an advantage in this case but it can be useful to know that your code block will run at least once regardless of whether the condtion holds.

Steven Good
Steven Good
2,303 Points

Thanks, Daniel. Understood. Thanks for your prompt answer. The course is suggesting that 'do while' is more advantageous than the 'while' loop. So, am I correct in thinking that I should always use 'do while' or 'for' loops (and avoid 'while' loops) or does it depend on the situation? Thanks again.

Steven Parker
Steven Parker
231,288 Points

Someone asked nearly this same question just two days ago.

I pointed out that any potential "advantage" to one method over the other will depend on the specific situation. They are both just tools in your toolbox, pick the right one for each job.

Steven Good
Steven Good
2,303 Points

Thanks Steven, I've checked out your answer.