background
|
|
Tombstone
Tombstone Tuning
Home of tuning, projects and fast cars and boats.
More icon

Games
Ruffnecks Gaming Gaming for everyone
More

Tombstone
Tombstone Tuning Home of tuning, projects and fast cars and boats.
More
|
|
Up Image
Navigation
Search this Site
Enter your search terms

Site Breadcrumb - You are here
|
Thau's JavaScript Tutorial
Lesson 4

by Thau!

Page 5 — For Loops

The while loop from the last example, which was written like this ...
var a_line="";
var loop = 0;
while (loop < width)
{
	a_line = a_line + "x";
	loop=loop+1;
}

... could have been written using a for loop, like this:

var a_line="";
for (var loop=0; loop < width; loop++)
{
	a_line = a_line + "x";
}

for loops come in this form:

for (initial value; test; increment) 
{
	do this stuff;
}

So, the above for loop sets loop = 0 and says that you should keep adding 1 to loop as long as loop < width. It's just like the previous while loop with a few less lines. Both say "add an x to a_line width times."

One more thing about loops before we do an exercise using them: Loops can be nested. Here's an example of nested loops.

next page»