16.3. Creating Loops Easily-From the Inside Out

 < Free Open Study > 

16.3. Creating Loops Easily From the Inside Out

If you sometimes have trouble coding a complex loop which most programmers do you can use a simple technique to get it right the first time. Here's the general process. Start with one case. Code that case with literals. Then indent it, put a loop around it, and replace the literals with loop indexes or computed expressions. Put another loop around that, if necessary, and replace more literals. Continue the process as long as you have to. When you finish, add all the necessary initializations. Since you start at the simple case and work outward to generalize it, you might think of this as coding from the inside out.

Suppose you're writing a program for an insurance company. It has life-insurance rates that vary according to a person's age and sex. Your job is to write a routine that computes the total life-insurance premium for a group. You need a loop that takes the rate for each person in a list and adds it to a total. Here's how you'd do it.

Cross-Reference

Coding a loop from the inside out is similar to the process described in Chapter 9, "The Pseudocode Programming Process."


First, in comments, write the steps the body of the loop needs to perform. It's easier to write down what needs to be done when you're not thinking about details of syntax, loop indexes, array indexes, and so on.

Step 1: Creating a Loop from the Inside Out (Pseudocode Example)
-- get rate from table -- add rate to total

Second, convert the comments in the body of the loop to code, as much as you can without actually writing the whole loop. In this case, get the rate for one person and add it to the overall total. Use concrete, specific data rather than abstractions.

Step 2: Creating a Loop from the Inside Out (Pseudocode Example)
 rate = table[ ]       <-- 1 totalRate = totalRate + rate 

(1)table doesn't have any indexes yet.

The example assumes that table is an array that holds the rate data. You don't have to worry about the array indexes at first. rate is the variable that holds the rate data selected from the rate table. Likewise, totalRate is a variable that holds the total of the rates.

Next, put in indexes for the table array:

Step 3: Creating a Loop from the Inside Out (Pseudocode Example)
rate = table[ census.Age ][ census.Gender ] totalRate = totalRate + rate

The array is accessed by age and sex, so census.Age and census.Gender are used to index the array. The example assumes that census is a structure that holds information about people in the group to be rated.

The next step is to build a loop around the existing statements. Since the loop is supposed to compute the rates for each person in a group, the loop should be indexed by person.

Step 4: Creating a Loop from the Inside Out (Pseudocode Example)
For person = firstPerson to lastPerson    rate = table[ census.Age, census.Gender ]    totalRate = totalRate + rate End For

All you have to do here is put the for loop around the existing code and then indent the existing code and put it inside a begin-end pair. Finally, check to make sure that the variables that depend on the person loop index have been generalized. In this case, the census variable varies with person, so it should be generalized appropriately.

Step 5: Creating a Loop from the Inside Out (Pseudocode Example)
For person = firstPerson to lastPerson    rate = table[ census[ person ].Age, census[ person ].Gender ]    totalRate = totalRate + rate End For

Finally, write any initializations that are needed. In this case, the totalRate variable needs to be initialized.

Final Step: Creating a Loop from the Inside Out (Pseudocode Example)
totalRate = 0 For person = firstPerson to lastPerson    rate = table[ census[ person ].Age, census[ person ].Gender ]    totalRate = totalRate + rate End For

If you had to put another loop around the person loop, you would proceed in the same way. You don't need to follow the steps rigidly. The idea is to start with something concrete, worry about only one thing at a time, and build up the loop from simple components. Take small, understandable steps as you make the loop more general and complex. That way, you minimize the amount of code you have to concentrate on at any one time and therefore minimize the chance of error.

 < 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