Controlling the flow of your program
5.7 Obsolete forms of control statements
During the period of almost 40 years since the first Fortran system was developed a great deal has been learned about programming style, and its effect upon programming efficiency and the reliability of programs. One of the most obvious effects of this learning process has been the development of new approaches to the ways of controlling the flow of programs. As a result, Fortran 90 contains a number of additional control constructs and statements whose use we do not recommend, but which may, nev'ertheless, frequently be encountered in older programs. Most of these are now so rare that they will not be described here, although a brief description will be found in Appendix E. One of these older constructs is, however, briefly mentioned here, owing to its widespread use in Fortran 77programs, although it must be emphasized that its use in new programs is strongly discouraged.
This statement is the computed GOTO,which has been replaced by theCASEconstruct, and, to a lesser extent, by the blockIFconstruct. It consists of a statement of the form
GOTO (labeCl ,label_2, ... ) ,integer_expression
162 Controlling the flow of your program
'and caused ,a transfer of control to one of the statements identified by the labels specified in the parenthesized list; It is described in more detail, for reference, in Appendix E. Its use in new programs is, however, strongly discouraged.
As well as the computed GOTO, Fortran 90 contains two even older, and even less desirable control constructs which are relics from its long past; these are the arithmetic IF and the assigned GOTO.
Neither of these should be used in new programs, and both had largely fallen into disuse by the time that Fortran 90 was released. They are briefly described in Appendix E for reference.
SELF-TEST EXERCISES 5.2
1 What is the main difference between aCASEconstruct and a block IF construct (apart from their syntax)?
2 What restrictions, if any, are there on the case expression in aSELECT CASE statement?
3 What forms may a case selector take? Are there any restrictions on any of these forms?
4 What is meant by overflow on a computer?
5 In a multiple choice situation, when should you use aCASEconstruct, and when should you use a block IF construct?
SUMMARY
• The ability of a computer program to choose which one of two or more alternative sequences of statements to obey is a major factor in making computers such powerful tools.
• The blockIF construct and theCASEconstruct provide the means for a program to select one of several alternative courses of action.
• The logical IF statement provides a simpler alternative to the blockIF construct in a limited number of cases.
• Relational operators are used to derive logical values from a compa(ison of two numeric expressions or two character expressions.
• 'Character expressions are compared by the relation operators using the Fortran collating sequence.
• Character expressions may be compared using the ASCII collating sequence by using special intrinsic functions.
• Logical operators are used to combine two logical values, and thus to allow more complex comparisons.
• Logical variables take one of two values: .TRUE. or .FALSE.
Programming exercises 163
• Fortran 90 syntax introduced in Chapter 5:
Variable declaration BlockIF construct
CASE construct
Logical IF statement Relational operators
Logical operators
:i LOGICAL :: list of variable names
IF (logicaLexpression) THEN
block_of-code
ELSE IF (logicaLexpression) THEN
block_of-code .
ELSE
block_of-code
END IF
SELECT CASE (case_expression)
CASE (case_selector) block_of-code
CASE DEFAULT
block_of-code
END SELECT
IF (logical_expression) Fortran_statement
>, >=, <=, <, ==, /=
• GT ., • GE ., . LE ., . LT ., . EQ ., . NE . .AND., .OR., .EQV., .NEQV., . NOT .
PROGRAMMING EXERCISES
~5.1 Write a program which will request a number to be typed at the keyboard and will then inform the user whether the number is positive, negative or zero.
Now modify your program so that if it used a block IF construct it now uses a
CASE construct, and vice versa. .
5.2 . Write a function that does not use any intrinsic functions which will determine the larger of two numbers.
5.3 Write a program to print out the truth tables for .OR., • EQV. and .NEQV. in the same form as the following table for .AND .
A B A.AND.B
T T T
T F F
F T F
F F F
164 Controlling the flow of your program
where the value for the third column is printed as the result of executing a logical expres'sion (and not by working out the result and simply printing the table!).
5.4 The logical NAND operation is the equivalent of performing the .AND. operation on two operands, followed by a .NOT. operation on the result. Thus
a NAND b is the same as not(a and b)
Write a logical function to perform the NAND operation on its two logical arguments.
5.5 Write a program which reads a number between 1and 6 from the keyboard and prints out the corresponding word: .'one', 'two' etc. If a number outside this range is typed the program should print an appropriate message.
Now modify your program so that if it used a block IF construct it now uses a
CASE construct, and vice versa.
5.6 Write a program that accepts a positive integer as its input and informs the user of all the following:
(a) whether the number is odd or even (b) whether it is divisible by seven
(c) whether it is a perfect square (that is, its square root is a whole number).
Modify your program to find the first even number that is divisible by 7 and is a perfect square.
5.7 Write a program that will determine how much income tax a person pays, given the following basis for taxation:
Income
5.8 It is often difficult to compare the value of items priced in different currencies.
Write a function to convert an amount in anyone of the eight currencies shown below to an equivalent amount in one particular currency, which we shall call the standard currency.
Use this function in a program which reads two amounts in any two of these currencies and calculates which is the lower.
Use the following table to specify the currencies and their relationships:
1UK pound
Programming exercises 165 5.9 The current I drawn by an electrical appliance of power P watts from a supply
voltage V volts is given by the formula:
I = VampsP
An electical supplier stocks three types of cable, suitable for currents of up to 5 amps, 13 amps and 30 amps, respectively. Write a program that asks the user for the power rating and supply voltage of an appliance, and displays the most suitable cable, or a warning if the appliance cannot be safely used with any of the cables in stock.
'"5.10 A firm produces digital watches and sells them for £15 each. However it gives a discount for multiple orders as follows:
Number ordered
Write a program to input the number of watches required and to print the gross cost, the discount (if any), and the net cost.
5.11 The brightness of a binary star varies as follows. At time t
=
0 days its magnitude is 2.5, and it stays at this level until t= 0.9 days. Its magnitude is then determined by the formula3.355 -In(1.352
+
COS(7T(t - 0.9)/0.7))until t
=
2.3 days. Its magnitude is then 2.5 until t=
4.4 days, and it is then determined by the formula3.598 -In(1.998
+
COS(7T(t - 4.4)/0.4))until t= 5.2 days. It then remains at 2.5 until t= 6.4 days, after which the cycle repeats with a period of 6.4 days.
Write a program which will input the value of the time tand print the brightness of the star at that time.
5.12 Exercise 4.13 required the writing of a subroutine to calculate the point of intersection of two lines, but ignored the problem of lines which were parallel and did not, therefore, intersect. In a similar way to that used in Example 5.3, modify your solution for Exercise 4.13 to return an error flag to indicate whether it ;"as possible to calculate the coordinates of the point of intersection.
5.13 Write a function which has two dummy arguments, first-JIerson and second-JIerson, both of a derived type which contains, among other things, the first and last names of a person, the age of the person, and the sex of the person. The function
166 Controlling the flow of your program
should return an integer value indicating the relationship between the two people represented by the arguments, according to the following rules:
• If the last names are the same then they are related, otherwise they are unrelated, and the function result should be zero.
• If they are related then a difference in age of over 20 years indicates a parent-child relationship; a difference of less than 20 years, and both ages over 20 implies a marital relationship; a difference of less than 20 years, and at least one aged 20 or less, implies a sibling relationship.
The value of the function should then be as follows:
Husband-wife 1
If the person represented by the first dummy argument is the older then the result is as shown; if that person is the younger then the value is negated (that is, daughter-father is returned as -3).
. Test your functio~ in a program which either reads two sets of personal details from the keyboard or has them as initial values in the main program and uses the function to cause an appropriate message to be printed, along the following lines:
Sarah Ellis is the daughter of Miles Ellis
5.14 Write a program which reads three real numbers representing three distances. The program should use these as the arguments to a subroutine which will set three further arguments as follows:
triangle is set true if the three distances could represent the sides of a triangle; that is, no number is greater than the sum of the other two numbers
isosceles is set. true if triangle is true and exactly two of the sides are of equal length; that is, an isosceles triangle
equilat is set true if triangle is true and all three sides are of equal length;
that is, an equilateral triangle
The program should then display an appropriate message.
5.15 Write a logical function which has two CHARACTERarguments, and which returns the value true if the first argument contains the second, and false otherwise. Thus, if the fun.ction is called within, then
within("Just testing", "test") is true, while
within("Just testing", "Test")
is false. (Hint: one of the intrinsic functions will help here.)
Programming exercises 167 Test your function with a driver program which inputs pairs of character strings
from the keyboard, and uses the result of a function reference to cause one of the following forms of message to be displayed:
(a) Thephrase' test' is containedwithin' Just testing' (b) Thephrase' Test' is not containedwithin' Just testing'