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, Second Edition now with O’Reilly online learning.

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