|
The for Loop
The for loop is perhaps the most widely used of all of the
loops available to JavaScript. It was designed to be close
in functionality to the for loop available to the C and C++
languages. The main difference is its treatment of data
types. As mentioned in previous discussions JavaScript does
its own data type conversions, making it extremely simple to
work with more than one data type.
The for loop is made of two separate parts - the condition
and the statement, as shown below in the syntax example.
for (initialValue ; condition ; adjustInitialValue)
{
statement
}
The condition of the for loop is the three values
(initialValue, condition, and adjustInitialValue) within the
opening and closing brackets after the for keyword. The
statement is the statement given within the curly braces.
The statement is executed on the variable declared for
initialValue and then on adjustInitialValue. The loop
iterates through itself until the condition is satisfied, no
matter how long it takes. Care should be taken to not create
an infinite loop that has a condition that won't ever be
satisfied by the rest of your script.
The initialValue is a variable that defines the initial
starting value for the loop. This variable can be defined
within the loop or by a separate variable declaration not
within the loop. Take care that the variable is of the
Global type of variable - JavaScript does not allow the use
of a Local type of variable outside of the function it was
created in. The condition is the section in which you'll use
the operators learned in a previous discussion. It is used
to provide the amount of times to iterate through the loop.
The third section of the loop is adjustInitialValue, which
is used to make an adjustment to the initialValue section.
This adjustment may be any increment, decrement, or any
other operation you may have a use for. Obviously, the
adjustment would reflect the type of data you are using and
the outcome you were looking for.
The below example shows the use of a loop very well. It is
used to print the multiplication table of 5, up to 5 X 12.
for
(var varOne = 0 ; varOne <= 12 ; varOne++)
{
document.write("5 X " + varOne + "=" + 5*varOne + "<br>");
}
The example shows the for keyword followed by the initial
value within the newly created variable, varOne. The value
of varOne is initially zero. The next part of the statement
tells you that the maximum amount of times the loop is to
iterate is 12 times, as indicated by the "varOne <= 12"
statement. Next comes the statement that changes the initial
value of zero within varOne. In this case the change is a
simple post-decrement operator that increases the value of
varOne by One. After each iteration, the document.write
statement is printed to the screen. The document.write
statement does the multiplication used to display the
multiplication by five results, not the loop condition. The
output looks as follows.
5 X 0=0
5 X 1=5
5 X 2=10
5 X 3=15
5 X 4=20
5 X 5=25
5 X 6=30
5 X 7=35
5 X 8=40
5 X 9=45
5 X 10=50
5 X 11=55
5 X 12=60
You can see now how simple it is to create a for loop. This
is most likely the reason why it is used so often.
Loops
JavaScript Introduction
The while Loop
The JavaScript Chronicles
JavaScript Introduction
Part 2: Data Types
Part 3: Arrays
Part 4: Operators
Part 5: Conditional Statements
Part 6: JavaScript Functions
Part 7: Pattern Matching - The RegExp Object
Part 8: Introduction to Server Side JavaScript
Part 9: Server Side JavaScript Mail Sending
Part 10: Server Side JavaScript and File Manipulation
Part 11: Working with Forms in JavaScript
Part 12: Getting to Know Dynamic HTML
|