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 Review while loops, do...while loops, and Loop Conditions

why are do/while statements automatically false?

During the quiz it asks a question about a code that looks something similar to this:

do (false){ document.write("Hello"); }while { document.write("Goodbye"); }

I understand that this will print 'Hello' at least once. And, determined by the answer, this will print:

Hello

Goodbye

But why is it that these loops are automatically false, or am I missing something?

4 Answers

:point_right: Your code produces a syntax error. How is that answer passing?! :confused:

I tried your code snippet in the Developer Tool's Console. It doesn't work...

Did you mean:

do {
  document.write("Hello");
} while (false);

or something?

Sorry I referencing the question off of the top of my head. That is the one, though.

Ok, so this program prints "Hello" once because the do part executes the code once, then it will keep running until the condition is false. I made it so it is already false, so it will just exit out.

So, to be clear... every thing begins as true?

No... Remember: The do while loop executes the code in the do even if the condition starts off as false. Then, after that, it's basically a standard while loop after that is done. If the condition is true, it will run again. If it's false, it will stop. It will keep running until the condition is false.

Thank you! You helped me very much in understanding this!