Correspondence Between Loops and Arrays

Cross-Reference

For further discussion of the correspondence between loops and arrays, see Relationship Between Data Types and Control Structures.

Loops and arrays are often related. In many instances, a loop is created to perform an array manipulation, and loop counters correspond one-to-one with array indexes. For example, these Java for loop indexes correspond to the array indexes:

Example 16-34. Java Example of an Array Multiplication

for ( int row = 0; row < maxRows; row++ ) {
   for ( int column = 0; column < maxCols; column++ ) {
      product[ row ][ column ] = a[ row ][ column ] * b[ row ][ column ];
   }
}

In Java, a loop is necessary for this array operation. But it's worth noting that looping structures and arrays ...

Get Code Complete, 2nd Edition 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.