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 trialRobert Mews
11,540 PointsDo/While and While Loops?
I probably asked this already, but really struggling with which to use. In a previous section, Dave used the do/while loop to display a prompt to the user and suggested to use that loop for when you want to send a user a prompt.
However, in this video, Dave is using a prompt and using the while loop. I'm uber confused now. I'm currently working on the last section of this JavaScript course (Student Record Search) and don't know which option to use...
2 Answers
Igor Yamshchykov
24,397 PointsThe main thing to understand, I think is this: The do while loop is executed at least once so if you'll have code like this:
var isCreate = false;
var count = 0;
while(isCreate){
count ++;
}
the result in varibale count by the end will be 0 but if you will use do while like so
var isCreate = false;
var count = 0;
do {
count ++;
}
while (isCreate);
the result for variable count will be 1,
because this loop will execute the code block once, before checking if the condition is true. In general you need to decide by yourself what loop to use, you just need to remember the difference between them.
Iain Simmons
Treehouse Moderator 32,305 PointsIn this case, the conditional used for the loop isn't important since there are more conditionals being used within, and the only way the loop stops is with the break
statement, not with a condition becoming false, so it doesn't make a difference which is used, but of course while
is shorter, by a little bit!