This chapter describes statements which were not included in previous chapters. These statements are of secondary importance; some are obsolete, and retained only to ensure compatibility with earlier software; others are highly specialized and likely to be needed only under very unusual circumstances.
The statements are:
11.1 ASSIGN STATEMENT
ASSIGN is an executable statement that assigns an integer constant to a special type of integer variable. The integer constant must be a valid statement label. The integer variable can then be used as a transfer destination in an as signed GO TO statement or a format specifier in a READ or WRITE statement.
The ASSIGN statement takes the form:
ASSIGN label TO name
where label is a valid statement label and name is an integer variable. Once an ASSIGN statement is executed, name has the meaning of a statement label and is not a regular integer variable. Therefore, operations cannot be performed on name while it has the meaning of label.
The following are valid ASSIGN statements:
ASSIGN 10 TO NUMBER
ASSIGN 20 TO IFORMAT1
These examples assign the value 10 to integer NUMBER and 20 to integer IFORMAT1. NUMBER is then used as a transfer destination in an assigned GO TO statement, and IFORMAT1 is used as a format specifier.
The following example program shows the use of the ASSIGN statement. The program increments a real variable X by 3. every time a pass through the IF-GO TO loop is completed.
Running this program yields:
FOR K= 1, X IS= 3.00
FOR K= 2, X IS= 6.00
FOR K= 3, X IS= 9.00
FOR K= 4, X IS= 12.00
FOR K= 5, X IS= 15.00
Example Program: Assign Statement and Assigned GO TO
11.2 COMPUTED GO TO STATEMENT
The computed GO TO is an executable statement that transfers control to one of several statement labels in cluded in its list, depending on the value of an integer expression which follows immediately after the list.
The computed GO TO takes the form:
GO TO (lab1, lab2, ... , labn) integexp
where lab1, lab2, ..., labn are a list of n valid statement labels in the program unit, and integexp is an integer expression that yields values from 1 to n. When the computed GO TO is executed, control is transferred to the statement label indicated by integexp. For instance, if integexp= 2, control goes to lab2, which is the second listed label.
The following example illustrates the use of the computed GO TO statement.
GO TO (10,20,30,40,50) I
If I= 1 during execution, control is transferred to statement 10; if I= 2, control is transferred to statement 20; and so on.
Typically, the GO TO and computed GO TO statements are disruptive, as shown in the example below. Therefore, their extensive use is discouraged. Usually, the same objectives can be accomplished in a more orderly manner with a DO loop/block IF construct combination.
Example 1: Computed GO TO Statement
DATA X,I /0.,0/
5 I= I + 1
IF(I.GT.3) GO TO 40
GO TO(10,20,30) I
10 X= X + 2.5
GO TO 5
20 X= X + 4.7
GO TO 5
30 X= X + 8.3
GO TO 5
This example initializes X = 0. and I = 0 with a DATA statement. Then, it sets an IF-GO TO loop to vary I from 1 to 3, each time adding a different real number to X. In this example, ... stands for a block of statements. Notice that five GO TO's were used in this example.
Example 2: DO Loop and Block IF Construct
This example accomplishes the same task as Example 1, but using a more friendly DO loop and block IF construct.
DO I= 1,3
X= X + 2.5
X= X + 4.7
X= X + 8.3
Notice the absence of GO TO statements in this example. It is good programming practice to minimize the use of GO TO and computed GO TO statements to improve readability.
11.3 BACKSPACE STATEMENT
The BACKSPACE is an executable statement that moves back to the beginning of the preceding record in an open input/output file, i.e., it repositions to the start of the last record transferred, enabling a record to be used more than once in an input/output operation. An example is:
where 90 is a statement label to which control is transferred in the event of an error during input/output operation. When no error specifier appears, the BACKSPACE statement reduces to:
Example Program: Use of Backspace Statement
This example reads a real number X from an input file named X.DAT. (Assume that X = 35.) Then, it adds to X an integer varying from 6 to 12, a total of seven times, each time backspacing and reading X again from the input file. Finally, it writes the result to an output file named X.OUT connected to unit 7. Note that X is being redefined twice at every pass of the loop, once during the READ statement and again during the arithmetic assignment statement (X= X + J).
The input would look like this:
The output would look like this:
J= 6 X= 41.00
J= 7 X= 42.00
J= 8 X= 43.00
J= 9 X= 44.00
J= 10 X= 45.00
J= 11 X= 46.00
J= 12 X= 47.00
11.4 BLOCK DATA SUBPROGRAM
A BLOCK DATA subprogram is used specifically for the purpose of initializing COMMON blocks with DATA statements. It is a special type of subprogram that can contain only specification statements. Any of the following statements can appear in a BLOCK DATA subprogram:
As with any other program unit, the last statement of a BLOCK DATA subprogram must be an END statement.
The following example uses a BLOCK DATA subprogram to initialize two integer arrays A and B with DATA statements. Then it adds A and B and writes the result (C) in the main program. Note the following features of this program:
Example Program: Use of Block Data Subprogram
11.5 EQUIVALENCE STATEMENT
The EQUIVALENCE is a nonexecutable statement that associates two or more variables in the same program unit with the same storage location. It takes the form:
in which var1, var2, and var3 are variables stored in the same location in memory. Therefore, an assignment of var1 causes the same assignment to var2 and var3. Dummy arguments cannot be specified in EQUIVA LENCE statements.
With the large storage space of current computers, this statement has now become obsolete, and it is retained only for compatibility with older software.
11.6 IMPLICIT STATEMENT
Recall the rules for integer and real type declaration stated in Chapter 3:
The IMPLICIT statement overrides these rules. When ever needed, the IMPLICIT statement is placed at the top of the specification block (see Fig. 1.1). The IMPLICIT statement has effect on variables only, and not on the default types of intrinsic functions.
The use of the IMPLICIT statement is illustrated by the following examples.
IMPLICIT INTEGER (I,J,K,L,M,N)
IMPLICIT REAL (A-H,O-Z)
These statements state the default setting for implicit declaration of data types. Therefore, they are redundant. In the absence of an IMPLICIT statement, the processor assumes that this is the case.
The first example above can also be written as
IMPLICIT INTEGER (I-N)
However, if a range is specified, as in (I-N), the first letter should precede the second letter in alphabetical order.
The following examples illustrate other rules for implicit type declaration.
IMPLICIT REAL (A,B,E-H,M-W)
IMPLICIT INTEGER (I-K)
IMPLICIT DOUBLE PRECISION (D)
IMPLICIT COMPLEX (X-Z)
IMPLICIT LOGICAL (L)
IMPLICIT CHARACTER*20 (C)
These examples state that, in the absence of explicit type declarations (e.g., INTEGER, REAL, CHARACTER, and so on), the processor takes variables beginning with A and B as real, C as character, D as double precision, E through H as real, I through K as integer, L as logical, M through W as real, and X through Z as complex.
11.7 PAUSE STATEMENT
The PAUSE statement is an executable statement that temporarily suspends program execution and displays a message on the terminal. It has the form:
in which the optional argument display is a label that tracks the particular PAUSE statement that suspended program execution (in case of an executable program having multiple PAUSE statements). The argument can be either a character constant or an integer consisting of up to 6 digits. If the argument is omitted, the following message is displayed on the screen:
The following examples illustrate the use of PAUSE:
PAUSE 'TAKE ACTION NO. 1'
In these cases, the following messages would be displayed on the screen, respectively:
TAKE ACTION NO. 1
Following a pause, to resume or terminate program execution, the appropriate command would have to be entered at the operating system level.
Example Program: Use of Pause Statement
The output from this program may look like this.
BEFORE FIRST PAUSE
BEFORE SECOND PAUSE
BEFORE THIRD PAUSE
TAKE ACTION NO. 1
AFTER THREE PAUSES--THE END
In this example, CONTINUE is the appropriate operat ing system command to resume execution of the program; $ is the operating system prompt.
11.8 REWIND STATEMENT
The REWIND statement is an executable statement that repositions an open file to the beginning of the file. An example is:
where 5 is a logical unit number, and 95 is a statement label to which control is transferred in the event of an error during input/output operation. When no error specifier appears, the REWIND statement, like the BACKSPACE statement, is reduced to:
|Documents in Portable Document Format (PDF) require Adobe Acrobat Reader 5.0 or higher to view; download Adobe Acrobat Reader.|
FORTRAN FOR SCIENTISTS AND ENGINEERS VICTOR M. PONCE • ONLINE EDITION •
|Copyright © 2014 • Victor M. Ponce • All rights reserved.|