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.

Steven Porter
3,097 Pointswhy 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

Alexander Davison
65,456 Points
Your code produces a syntax error. How is that answer passing?!
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?

Steven Porter
3,097 PointsSorry I referencing the question off of the top of my head. That is the one, though.

Alexander Davison
65,456 PointsOk, 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.

Steven Porter
3,097 PointsSo, to be clear... every thing begins as true?

Alexander Davison
65,456 PointsNo... 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.

Steven Porter
3,097 PointsThank you! You helped me very much in understanding this!