10.9 for Loop

In the previous section, we used a while loop to access all the checkboxes in the collection of job skills checkboxes. Using a while loop works OK, but in this section, we use a for loop to access the checkboxes, which leads to a more compact implementation.

FIGURE 10.13 shows the while loop used in the Job Skills web page and a functionally equivalent for loop. Both versions use a counter variable, i, that gets initialized to 0 and gets incremented each time through the loop. With a for loop, the counter mechanism is implemented within the loop’s heading. It’s such a foundational part of a for loop that the counter variable is given a special name—an index variable. Sound familiar? Yep, an index variable is also the name we ...

Get Web Programming with HTML5, CSS, and JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.