Looping through Arrays

Obviously if you have an array of any substantial size, then working with its individual elements—as in the previous examples—quickly becomes tedious. This is even truer if the same process is used on each value (printing it, performing calculations with it, and forth). The easiest way to access every array element is to use a for loop.

You can review Chapter 4, “Control Structures,” for the syntax of a for loop, but normally there are four parts (Figure 6.6). The loop first initializes a variable to a value, and then checks that variable against another value as its condition. If that condition is true, certain statements are executed. Finally, the variable is incremented.

Figure 6.6. This flowchart reflects how for loops ...

Get C Programming: Visual Quickstart Guide now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.