August 18, 2009

Javascript Tutorial 11: Loop

For Loop

You have to define a variable and most of the time, people use an integer of 1 or 0.
var i=0
for (i=0;i<5;i++)

Syntax for "for loop" is the word "for". You have to add the first value defined by the variable. Then you have to put a semicolon to seperate it from the other. Then you type in the range or how far you want your variable to reach. Then define it so that javascript adds by one.

Remember that ++ tells javascript to add one.

var i=0
for (i=0;i<5;i++)

Inside the brackets are the code that you are going to put on the loop. This is going to list numbers in each line up to five.

While Loop

var i=0;
while (i<=10)
document.write("The number is "+i);

This allows you to run the function certain amount of time. i=i+1 stops the variable from going all the time.

, , , ,

Powered by ScribeFire.

No comments:

Post a Comment