Repeating parts of your program
6.2 Count-controlled DO loops
The first statement of aDO loop is called aDO statement and, as we have already seen, takes one of the forms:
DO count=initial, final, inc
DO count=initial, final DO
The first two alternatives define a count-controlled DO loop in which an integer variable, known as the DOvariable, is used to determine how many times the block of statements which appear between the DO statement and the END DO statement are to be executed. We shall discuss the third alternative in Section 6.3.
There are also two other forms of the DO statement which we do not advocate using, but which will be briefly discussed in Section 6.6. .
Informally, we can consider the second, slightly simpler, form, in which inc is absent, as meaning that the loop is executed for count taking the value initial the first time that the loop is executed, initial
+
1the. next time, and so on until it takes the value final oli the last pass through the loop.In a similar manner, we can informally consider the first form to mean that the loop is executed for count taking the value initial the first time that the loop is executed, initial
+
inc the next time, and so on, with the value of count being incremented by inc for each subsequent pass; in this case the final pass through the loop will be the one which would result in the next pass having a value of count greater than final.DO statement
Figure 6.2 Some examples of DOstatements and their effect.
The formal definition of this process is that when the DO statement is executed an iteration count is first calculated using the formula
MAX ((final-initial+inc) / inc,0)
and the loop executed that many times. On the first pass the value of count is initial, and on each subsequent pass its value is increased by inc. If inc is absent then its value is taken as1. The effect of the MAX function is that if final
<
initial and inc>
0then the iteration count will be zero, and the statements in the loop will not be executed at all. Notice, however, that count will be set to the value initial since this assignment takes place before the iteration count is tested.The DO variable, count, must be an integer variable, while initial, final and inc must be integer expressions.
Because of its special role, it is not permitted to alter the value of the DOvariable between the initial DO statement and the corresponding END DO statement by any means other than the automatic incrementation which is part of the DOloop processing.
Figure 6.2 shows some examples of the iteration counts for a number of different DO statements, and the values that will be taken by the DO variable on each pass through the corresponding loops, and it can be seen that our informal description is perfectly adequate as long as care is taken over the last value. It must always be remembered, however, that the way that a loop works is not by looking at the value of the DO variable on each pass, but by calculating the iteration count once and then decrementing it by one after each pass is completed. One effect of this is that once the loop has been completed (that is, it has been executed the number of times defined by the iteration count) the DO
variable will have the value that it would have had on the next pass through the loop, if there had been one. Another effect is that if the values of initial, final and, if it is present, inc are such as to result in a zero or negative value for the iteration count then the loop is not obeyed at all, because the value of the iteration count is examined immediately before commencing execution of each pass.
174 Repeating parts of your program
[!]
ProblemWrite a program which first reads the number of people siHing an exam. It should then read their marks (or scores) and print the highest and lowest marks, followed by the average mark for the class.
[!]
AnalysisThis is a straightforward problem which will use aDO loop to repeatedly read a mark and use it to update the sum of all the marks, the maximum mark so far, and the minimum mark so far.
-Data design
Purpose
Number of people (data) Mark (data)
Max and min marks Sum of all marks Average of all marks DO variable
Structure plan
Type INTEGER INTEGER INTEGER INTEGER REAL INTEGER
Name number mark
,maximum, minimum sum
average i
Since theDOvariable is only used to control the loop we will follow normal programming (and mathematical) conventions and use the name i for this purpose.
One aspect that must be very carefully considered is the initialization of the three variables which will be used to save the accumulated sum of the marks, and the maximum and minimum marks. In this example the cumulative sum must obviously start at zero, but what about the maximum and minimum marks? What
Count-controlled DO loops 175 we shall do (at steps 3.3 and 3.4) is to compare each mark that is read with the
highest (or lowest) read previously and store the higher (or lower) as the new maximum (or minimum). It follows, therefore, that initially the maximum must be set to a lower value than any actual marks can take, and the minimum must be set to a higher value than is possible as a mark. If marks are to lie, for example, in the range 0-100 then any values less than zero or greater than 100 could be used for the initial maximum and minimum, respectively. However, as the intrinsic function HUGE is a generic function we can use it to provide the largest possible integer value, which will be more than large enough!
Finally, as there a~e only three variables to be initialized we could use assignment statements, but as a general rule it is preferable to always carry out such initialization in the declaration statement as this causes the initial values to be stored when the program is first loaded, as well as making it clear that these are initial values which will be changed when the program is executed.