Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops `do ... while` Loops

Mirali Mirzayev
Mirali Mirzayev
1,980 Points

Difference between while and Do while

<CODE> var secret = prompt("Type in your Password") while(secret.toUpperCase()!=="RAW"){ secret = prompt("Try again"); } alert("You are in"); <END CODE>

The above code also displays a statement (Type in your password). So why would i need do While if this statement is also executed before a condition using regular while Loop.

Thank you.

3 Answers

Balazs Peak
Balazs Peak
46,076 Points

The only difference between them is the time of testing. "While" tests the statement at the beginning of each iteration, "do while" tests the statement at the end of each iteration.

Most related programming problems can be solved by both of them, but sometimes, one is more practical than the other.

For example: with do-while, the iteration will run at least once, no matter what. Even when the statement is false - since it is tested after the iteration.

Example:

var IamFalse = false;

do{
     print("This will be printed out at least once.");
} while(IamFalse)

You can try it yourself: http://rextester.com/DDLM33587

Balazs Peak
Balazs Peak
46,076 Points

What I'm getting from your code is that you are mixing the two things together. Maybe that is the cause behind the unexpected behaviour.

You either do this:

while(false){
 document.write("<p> This WILL NOT be printed out </p>");
}

Or you do this:

do{
 document.write("<p> This WILL be printed out </p>");
}while(false)

I've made a demonstration for you, check this out: https://codepen.io/bradib0y/pen/Xxbvjp

Mirali Mirzayev
Mirali Mirzayev
1,980 Points

Thank you for your response. What i really don't understand still is that, in both cases when i prompt it with Do or right with while simply like above i am able to put out a statement that collects an information and then the condition gets checked.

          <p>
var randomNum=getRandomNum(6);
var guess;
var guessCount=0;
var correctGuess=false;
function getRandomNum(upper){
  return Math.floor(Math.random()*upper)+1;
}

do{
  guess=prompt("What is the number that i am thinking of?");
  guessCount+=1;
  if(parseInt(guess)===randomNum){
    correctGuess=true;
  }
}while(!correctGuess){
  document.write("<h1> Congrats </h1>");
  document.write("<h1> Took you "+guessCount+" tries to get the number right </h1>");
}
</p>
          ```