[Total: 0    Average: 0/5]

Javascript Reserved Word – FOR

FOR loops in JavaScript allow for a set of statements
to be repeated until a condition is evaluated
to become a boolean TRUE. It also allows for
a repetitive rule set to be applied at each iteration.

The basic syntax for a FOR loop:

for ([start condition];[end condition];[repetitive rule set])

[{ [code;]+ }] | [code;]

Breaking this down a bit:

The “start condition” can be any number of valid javascript
statements until the first “;” (semi-colon) is reached.

The “end condition” can also be any number of valid javascript
statements between the first “;” and the second “;”.

The “repetitive rule set” is the javascript statement(s) that
follow the last “;”.

A very simple FOR statement that outputs the numbers from
1 to 10:

for (var _i=1;_i<=10;_i++)

This FOR loop declares a variable “_i” to be only valid
inside the for loop and preset to the value of 1.

The test for this loop to be finished is checked:

_i<=10 (_i is 1 and 1 is less than or equal to 10) so
the first iteration of the loop starts.

The document.write statement is the only statement inside
the loop since there are no curly braces for the body
of the FOR loop. I is displayed along with a BR tag (1<BR>)

After each iteration of the loop, the value _i is incremented by 1
(_i++). So the next evaluation for exiting the loop is tested:

_i<=10 (2<=10)… This continues until _i is 11 – at which point
the loop exits and the next javascript statement after the end
of the for loop is processed.

Times Viewed: 52

Leave a Reply

Your email address will not be published. Required fields are marked *