HTML and CSS Reference
In-Depth Information
Figure 12-13
nested for loops
rowNum
Counter
colNum
Counter
Code Written
to the Page
1
1
1
1
<tr>
<td> 1 , 1 </td>
<td> 1 , 2 </td>
</tr>
Nested for loop
1
2
2
2
2
2
<tr>
<td> 2 , 1 </td>
<td> 2, 2 </td>
</tr>
1
2
3
3
3
3
<tr>
<td> 3 , 1 </td>
<td> 3 , 2 </td>
</td>
1
2
The update expression is not limited to increasing the counter by 1. You can use the
other operators introduced in the previous tutorial to create a wide variety of increment
patterns. Figure 12-14 shows a few of the many different ways of updating the value of
the counter variable in a for loop.
Figure 12-14
counter values in for loops
for Loop
Counter Values
for (var i = 1 ; i <= 5; i++)
i = 1, 2, 3, 4, 5
for (var i = 5; i > 0; i--)
i = 5, 4, 3, 2, 1
for (var i = 0; i <= 360; i += 60)
i = 0, 60, 120, 180, 240, 300, 360
for (var i = 1; i <= 64; i *= 2)
i = 1, 2, 4, 8, 16, 32, 64
Using for Loops and Arrays
A for loop is often used to cycle through the different values contained within an array.
The general structure for accessing each value from an array is
for (var i = 0; i < array .length; i ++) {
commands involving array[i] [ i ]
}
where array is the array containing the values to be looped through and i is the counter
variable used in the loop. The counter variable in this case represents the index number
of an item from the array. The length property is used to determine the size of the array.
The last item in the array has an index value of one less than the array's length—because
array indices start with zero—so you continue the loop only when the array index is less
than the length value.
 
Search WWH ::




Custom Search