All about for loops

There are a few different types of loops, but the most commonly used type is called a "for" loop and that's what we'll focus on. A simple one looks like this:

for (x = 1; x < 11; x++) {      
     alert("I have now counted to: " + x);
}

for loops come in this form:

for (initial value; test; increment) {
     do something;
}

Even if you don't completely understand the way this is written, you may already have an idea how annoying this loop is. Regardless, please click the button below to see what loops are all about:

Dissecting the "for" loop

You can probably determine from our discussion of curly brackets, that the commands inside the curly brackets are controlled by whatever goes on in the "for" statement. So, the trickiest part of this command is understanding the "for" statement, which looks like this:

for (x = 1; x < 11; x++)

As you can see, the "for" command is followed by three mathematical or logical statements separated by semi-colons and enclosed in a single set of parentheses. Each of these three statements means something very specific:

The first statement creates a variable called "x" and sets it to a starting value of 1.

The next statement is treated as a logical statement and the loop will repeat as long as this logical statement evaluates to true. In this case, the logical statement is: "x < 11" and this means that the loop will repeat for as long as x equals a number less than 11.

The last statement tells how the variable "x" should change each time the loop repeats. The use of a double plus sign is a programming convention that just means "add one to this number." So the value of x goes up by one every time the loop repeats.

Each of the three statements following the "for" command relates to the value of the variable "x", so here's a one-line summary of what each of them does with the value of this variable. The "for" loop is a tough thing to memorize, so you may want to write this down as a cheat sheet to use when creating "for" loops.

for (set starting value; test for cut-off value; increase value by 1 each time)

The behavior of the script in the example above should start to become clear by now.

It begins by setting x equal to 1. Then it executes the alert() Method and tells everyone that it has counted to 1. Then it comes around the loop again and checks to see if x is greater than 10 yet. When it finds that x is still not greater than 10, it increases the value of x by 1 (so now x = 2) and once again it executes the alert() Method, this time announcing that it has counted to 2. This process continues with x increasing by one every time until x = 11 and because x is no longer less than 11, it finally fails the cut-off test and the loop quits.

Beware the infinite loop.

You may soon discover that it's always a good idea to be very sure that the cut off value is reachable. Consider the consequences of a loop like this:

for (x = 5; x > 2; x++)

Since it begins by setting x = 5, and it makes x greater by 1 each time through, x will never be less than 2, which is the cut-off condition for the loop. This is referred to as an infinite loop. It's a bad thing. You'll probably need to press Ctl+Alt+Del (PC) or Command-Period (Mac) to shut it down. If you've accidentally created an infinite loop in your program, you may be very confused why your machine is doing nothing and won't respond to any command. It's too busy looping, and looping....

Loops can be nested. Here's an example of nested loops.