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

do and while loops

i have been jamming on this question for quite some time. Please help me understand idea of do and while loops. Tried the video thought i had understood but seems like im not getting something

2 Answers

Tsenko Aleksiev
Tsenko Aleksiev
3,819 Points

OK, let's break it down:

  1. Do - while loop: please Mr. JavaScript DO this ...the code....WHILE ( ...this thingy is still true ), aka:

    var num = 0;
    do {
    console.log(num);
    num++; /* this is very important, if you don't have something to change in the loop aka the num variable you will get an infinite loop and your browser will crash. With num++ each time the loop goes the num variable is adding +1 to it */
    }while(num < 10); /* here you check if the condition is still true or false. If it's true, it goes back up and executes the code inside the curly brackets, if false - loop ends and the program continiues */;
    

    NOTE! The do - while loop executes at least 1 time aka the code inside is executed ones - it runs, does what's inside the block and than checks if condition is true or not.

  2. While loop: please Mr. JavaScript CHECK if this condition is true and IF it's true, do this, aka:

    var num = 10;
    while(num > 0){
    console.log(num);
    num--; /* same thing as the do-while loop, you need to change something in the block so that the condition eventually changes because otherwise you get an infinite loop and it crashes your browser again */
    }
    

    It's almost the same thing, but slightly different: you check your condition in the start, if it passes aka it's true, you execute the code in the block, if not, you skip everything in the curly braces and your program goes ahead. NOTE! If the condition of the while loop is wrong it never executes, see NOTE of the do-while loop.

That's it! :) Srry for my english I'm not that good at it :)

Thanks Tsenko Aleksiev . So in the last while loop that you gave me, the code never stops right cause as long as num>0 the console still brings out the contents of the variable num?? secondly if I put a false in the while loop what does that result to?

you make this look very simple hahahahaha.

Tsenko Aleksiev
Tsenko Aleksiev
3,819 Points

In the while loop I first check if 10 ( num ) is bigger than 0 - yes it is than bring it to the console, num becomes 9 ( num-- ), goes back up, is 9 bigger than 0, yes it is bring it to the console, num becomes 8 ( num-- ) and so on till num becomes 0, is 0 bigger than 0, no it's not, loop ends. If you put false in the condition of a while loop it just never executes, the program skips what's inside the block ( the curly braces of the while loop ) and just goes on. Hope I helped :)