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.
Coding a loop from the inside out is similar to the process described in Chapter 9.
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.
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.
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:
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.
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.
Finally, write any initializations that are needed. In this case, the totalRate variable needs to be initialized.
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.
3.141.200.3