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 trialIdan shami
13,251 PointsWhy should I use the do...while loop?
I still can't understand why should I use this type of loop... the explanation in the video wasn't clear enough... if someone will write an invalid e-mail address the code will run again until he writes a valid email address, why would I need the do while loop for that? It will work just fine with a regular while loop isn't it?
thanks, Idan.
2 Answers
Justin Cantley
18,068 PointsThe do...while loop will always run at least once and then check the condition after the first run. A while loop on the other-hand might not run at all if a certain condition isn't met at the start of the loop.
The reason you would want to use do...while instead of while for this e-mail exercise is because the condition that the loop is checking for is a valid e-mail address. This means you must first submit an e-mail address before the condition can be checked.
The code might look something like this:
do {
prompt("Enter your email address");
}while(/*email doesn't meet specific format*/)
This will run the code at least once, but if the format is incorrect it will continue to run until the condition is met.
Trying this same code with a while loop would look something like this:
while(/*email doesn't meet specific format*/) {
prompt("Enter you email address");
}
This won't work because the code won't run in the first place in order to check whether the e-mail address format is correct or not.
I hope this helps!
Dayana Guevara
Courses Plus Student 839 PointsI think i got it. The while loop won't ever run because you haven't asked for the user's email yet, so the condition is not met and hence the loop doesn't run.
But! there are ways to solve that, as in the following example:
while (answer !== "blue") { var answer = ""; answer = prompt("what color is the sky"); } document.write("You are correct!");
...That code gives me the same results as the following:
do { answer = prompt("what color is the sky"); } while (answer !== "blue")
document.write("You are correct!");
The only advantage I see is that you use a little less code. Please correct me if i am wrong!!
Axel Perossa
13,930 PointsYou are correct. As long as the variable "answer" has been declared before the condition check, it will work (and fail, thus executing the loop and triggering the prompt).
Idan shami
13,251 PointsIdan shami
13,251 Pointsok... I think I got it, but can't I just write an if condition in the while loop? if the email isn't valid just continue to ask for the email if the email is valid break from the loop...
Axel Perossa
13,930 PointsAxel Perossa
13,930 PointsThe second example would work too. If the variable doesn't hold an email, or even nothing at all (undefined), as long as it is declared the condition will evaluate to false and the loop would run, executing the prompt.
Aakash Srivastav
Full Stack JavaScript Techdegree Student 11,638 PointsAakash Srivastav
Full Stack JavaScript Techdegree Student 11,638 PointsYes...absolutely Axel Perossa.
We will use
!==
in the while loop condition. Then the code inside it will run until he enter the correct format. You should rethink about your second case of while loop you mentioned above. Correct me if i am wrong.