16.4. Correspondence Between Loops and Arrays

 < Free Open Study > 

Cross-Reference

For further discussion of the correspondence between loops and arrays, see Section 10.7, "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:

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 aren't inherently connected. Some languages, especially APL and Fortran 90 and later, provide powerful array operations that eliminate the need for loops like the one just shown. Here's an APL code fragment that performs the same operation:

APL Example of an Array Multiplication
product <- a x b

The APL is simpler and less error-prone. It uses only three operands, whereas the Java fragment uses 17. It doesn't have loop variables, array indexes, or control structures to code incorrectly.

One point of this example is that you do some programming to solve a problem and some to solve it in a particular language. The language you use to solve a problem substantially affects your solution.

cc2e.com/1616

Checklist: Loops

Loop Selection and Creation

  • Is a while loop used instead of a for loop, if appropriate?

  • Was the loop created from the inside out?

Entering the Loop

  • Is the loop entered from the top?

  • Is initialization code directly before the loop?

  • If the loop is an infinite loop or an event loop, is it constructed cleanly rather than using a kludge such as for i = 1 to 9999?

  • If the loop is a C++, C, or Java for loop, is the loop header reserved for loop-control code?

Inside the Loop

  • Does the loop use { and } or their equivalent to enclose the loop body and prevent problems arising from improper modifications?

  • Does the loop body have something in it? Is it nonempty?

  • Are housekeeping chores grouped, at either the beginning or the end of the loop?

  • Does the loop perform one and only one function, as a well-defined routine does?

  • Is the loop short enough to view all at once?

  • Is the loop nested to three levels or less?

  • Have long loop contents been moved into their own routine?

  • If the loop is long, is it especially clear?

Loop Indexes

  • If the loop is a for loop, does the code inside it avoid monkeying with the loop index?

  • Is a variable used to save important loop-index values rather than using the loop index outside the loop?

  • Is the loop index an ordinal type or an enumerated type not floatingpoint?

  • Does the loop index have a meaningful name?

  • Does the loop avoid index cross-talk?

Exiting the Loop

  • Does the loop end under all possible conditions?

  • Does the loop use safety counters if you've instituted a safety-counter standard?

  • Is the loop's termination condition obvious?

  • If break or continue are used, are they correct?


 < Free Open Study > 


Code Complete
Code Complete: A Practical Handbook of Software Construction, Second Edition
ISBN: 0735619670
EAN: 2147483647
Year: 2003
Pages: 334

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net