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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access