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