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 trialJosh Slabaugh
9,635 PointsI'm having trouble understanding the theory of the do while loops. Could someone explain what's happening in each step?
I'm having trouble understanding the whole true and false part specifically.
1 Answer
Jennifer Nordell
Treehouse TeacherHere's one of the quiz questions. Let's take a look at it and go through it... slowly
var x = 0;
do {
console.log('I love JavaScript');
x += 1;
} while ( x <= 3)
Ok so we start with x set to 0. Agreed? Now no matter what, this loop will always run at least once. So it does the console.log which prints I love Javascript to the console. That has now been printed a total of one time. It then increments x by one which makes x equal to 1. Is x less than or equal to three? Yes! So we do it again.
Now x is equal to 1 and we print I love JavaScript again bringing our prints to a total of 2. Then we increment x again making x equal to 2. Is 2 less than or equal to three? Yes! Do it again.
Now x is 2. We print our message again for a total of 3 prints to the console. Now we increment x to 3. Is 3 less than or equal to 3? Yes!
Now x is 3. We print our message again for a total of 4 prints to the console. Now we increment x to 4. Is 4 less than or equal to 3? No! Our do while loop exits.
Now the question asks you to fill in the number if you want to print 10 times. If we had a total of 4 prints when x is less than or equal to 3... what should x be less than or equal to if we want it to print 10 times? One less than the number we want to print out. Hope this helps!
Josh Slabaugh
9,635 PointsJosh Slabaugh
9,635 PointsThank you! That makes a lot of sense now, I appreciate the step by step. I'm still pretty new to programming but it kinda seems like the more you read your code as an actual English sentence the better off you are.