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 `do ... while` Loops

Idan shami
Idan shami
13,251 Points

Why 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
Justin Cantley
18,068 Points

The 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!

Idan shami
Idan shami
13,251 Points

ok... 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...

The 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
seal-mask
.a{fill-rule:evenodd;}techdegree
Aakash Srivastav
Full Stack JavaScript Techdegree Student 11,638 Points

Yes...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.

Dayana Guevara
PLUS
Dayana Guevara
Courses Plus Student 839 Points

I 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!!

You 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).