next up previous contents
Next: Expressions and Assignment Up: Data Objects Previous: Initialisation

 

Examples of Declarations

The following declarations show how careful choice of data type, name and inclusion of comments can help readability:

     CHARACTER(LEN=20) :: Location   ! Name of hospital
     CHARACTER :: Ward               ! Ward, eg, A, B, C etc
     INTEGER   :: NumBabiesBorn = 0  ! Sum total of births
     REAL      :: TimeElapsed = 0.0  ! Time since 1st birth (hours)
     REAL      :: MaxTimeTwixtBirths = 0.0 ! longest gap between births
     REAL      :: AveTimeTwixtBirths ! average gap between births
     REAL      :: TimeSinceLastBirth ! gap since the last birth
     LOGICAL   :: NHS                ! Is it an NHS hospital

There is nothing wrong with using verbose variable names and augmenting declarations with comments explaining their use.

It is important to use an appropriate data type for an object, for example, above the variable Location is the name of a hospital, it is clearly appropriate to use a CHARACTER string here. (We have assumed that the name can be represented in 20 letters.) The second variable, Ward only needs to contain one letter of the alphabet, this is reflected in its declaration. It is also a good idea to initialise any variables that are used to hold some sort of counter. In the above code fragment, we have 3 such examples: NumBabiesBorn is incremented by one each time there is a new birth, this value must always be a whole number so INTEGER is the appropriate data type; TimeElapsed measures the time in hours since the first birth, in order to be accurate, we need to be able to represent fractions of hours so a REAL variable is in order, when the program begins to run zero time will have elapsed which explains the initialisation; the final example is MaxTimeTwixtBirths the longest spell of time between births, again it is a good idea to initialise the variable to a sensible value due to the way it will proably be used in the program. It is likely that the following will appear;

     IF (TimeSinceLastBirth > MaxTimeTwixtBirths) THEN
       MaxTimeTwixtBirths = TimeSinceLastBirth
     END IF

The AveTimeTwixtBirths variable will also have to represent a non-whole number so REAL is the obvious choice. The variable, TimeSinceLastBirth is used to store the current time gap between births and is, in this sense, a temporary variable. LOGICAL variables are ideal when one of two values needs to be stored, here we wish to know whether the hospital is NHS-funded or not.

Now try this question gif

Return to corresponding overview page gif


next up previous contents
Next: Expressions and Assignment Up: Data Objects Previous: Initialisation

©University of Liverpool, 1997
Wed May 28 20:20:27 BST 1997
Not for commercial use.