Loops are, quite simply, blocks of code that execute over and over until a condition that you set becomes false. There are two different types of loops - for loops and while loops. They are virtually identical in what they do, except the for loops is a much more controlled loop environment in that it has all of the controls contained into it's syntax, whereas the while loop does not.

A note about loops. If you don't ensure that the loop has a way to close, you may encounter an endless loop, or loop that doesn't end. It could crash your computer, but more than likely your browser will hault it and tell you about it before it gets to that point.

For Loops

Here's the basic syntax for a for loop:

for(variable delcaration; condition; number operation)
{
  do this until the condition is false.  
}

The variable declaration is a place in which you can declare/initialize any variables that the loop will use. It is there for two reasons. It is expected that you use it in the conditions and number operation area of the for loop, and it ensures that you know what the variable is when the for loop begins. The condition is any of the coditions mentioned in the If Statements lesson. Lastly, the number operations part makes some kind of change to the variable used in the condition part. You can change the variables in the condition in the code for the for statement itself, but it's safer and much better coding practice to just contain these factors into the statement syntax. Here's a real for loop in action:

var array = new Array(1,2,3,4,5,6,7,8,9);

for(var i = 0; i < array.length; i++)
{
  array[i] *= 2;
}

This is a practice known as traversing the array. It visits each member of the array, and does something to it (in this case doubles it). Variable i starts out at 0, at which point it goes to array[0] and mulitplies it by 2. Then it checks to see if i is less than array.length. When it sees that it is, it incrememts i, then performs the operation again. It then goes to array[1] (i equals 1 now) and doubles it. It continues this until i is no longer less than the value of array.length. This whole statement will double each value in the array.

That is pretty much all you need to know about for loops. You can get infinitely creative with them with enough ingenuity with this knowledge, but you probably won't use for loops for much more than traversing arrays for a long time.

While Loops

While loops are utterly simple; almost to the point of being self explanatory. They simply say "While this is true, do this". Just look at it's syntax:

while(condition)
{
  do this.
}

While the condition is true, just keep doing whatever's in the brackets. The condition is any of the conditions mentioned in the last lesson. Remember! Make sure the loop finishes! This loop checks the condition first, so it's possible to execute this statement without even doing the code inside of it.

Do-While Loops

Do-while loops are identical to while loops, save for a few little changes. Among said changes is the fact that the do-while loop executes the code it contains at least once NO MATTER WHAT. Here's the syntax.

do
{
  do this
}while(condition);

One thing to note. This loop has a semicolon tacked on at the end of it for some reason. Don't forget about it!