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!
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
Vic Mercier3,276 Points
When would I couldn't use a while loop instead of the do while loop?
I don't really understand.
Loops can be confusing at first - the short answer is that you generally CAN use a different type of loop for most situations, depending on how you structure your code.
The main thing to keep in mind is that certain loop types can be easier to code/easier to read in certain circumstances. Do-while loops can be useful when you know you want the code to run at least once (a while loop will not run at all if the condition it checks for is false beforehand, but a do-while loop will run once before it checks). A for loop is one of the easier ones to read and organize when you want a loop to repeat a particular number of times using a counting variable. You can do this with a while loop too, but a for loop can look cleaner and more organized. Finally, a while loop is best when you want the code to run only when certain conditions are met, and stop when they're not.
As you keep coding, you'll start to see circumstances that fit the different loop types. Sometimes you'll run into a scenario where it's really just up to your preference.
Hopefully this helped clear things up a little bit - happy coding!