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

Learning Coding
7,134 PointsWhile Loop vs. Do While Loop
I have trouble wrapping my head around the do while loop. I understand that the condition is evaluated at the end in the do while loop while the condition is evaluated at the beginning in the while loop. What I don't understand is the value of using the do-while loop when the while loop can accomplish the same task. It is just an extra step. Could somebody give me an example of when you would use choose a do-while loop over a while loop? Thank you
3 Answers

Jacob Herrington
15,835 PointsThis Stackoverflow question basically answers your question. There are occasionally uses for it, but they are very rare. Usually, when a do-while loop could be used, there is also another solution.

Jennifer Nordell
Treehouse TeacherI agree with Jacob Herrington. I believe most programmers would tell you that most do while loops could be reconstructed as a while loop with proper setup in advance. I'm of the opinion that the reason this is shown is simply to give you another tool in your programmer's toolbelt. It is, obviously, valid to use this and may be used by other programmers you come across/work with. So knowing how to use it is useful if only for the sake of knowledge.
Hope this helps!

jayshi
5,950 Pointsdo-while loop is for something you need to execute first, then see if it's needed to execute again. Do-while loop can be used for authentication where user enter their credentials first, then to see if it is needed to be executed again. If you use while-loop for the authentication, you will first need to initialize values to satisfy the condition for while-loop to execute.