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 trialSteven 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,469 PointsYour 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,469 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,469 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!