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 For Loops

mrx3
mrx3
8,742 Points

Just finished the for loop video and example code. So basically a for loop is like a while and a do while loop?

These for loops are pretty sweet and easy to understand. I struggle with loops all the time and this for loop actually made sense. In the video Dave stated that for loops are like while and do while loops, is that a correct statement? Can I do what while, and do while loops do with a for loop, or are there somethings only while and do while loops can do?

Thanks in advance for any input/feedback....AND I LOVE FOR LOOPS :D

1 Answer

do while loops always execute a statement once before entering the loop. Unlike while and for loops, the statement inside their code block depends on the evaluation of the expression in the loop.

do {
     // this will execute at least once regardless of the result of the condition in the while
     alert("People who can't do something themselves will tell u u can't do it.");
} while (1 > 10);



while (1 > 10) {
    // BAM! this never happened, the condition is false.
       alert("People who can't do something themselves will tell u u can't do it.");
}