CHAPTER 11 

 

MISCELLANEOUS STATEMENTS

 

 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 spe cialized and likely to be needed only under very unusual circumstances. 

The statements are: 

  • ASSIGN 
  • Computed GO TO
  • BACKSPACE 
  • BLOCK DATA 
  • EQUIVALENCE 
  • IMPLICIT 
  • PAUSE, and 
  • REWIND. 
 

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 vari able 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 inte ger 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 NUM BER and 20 to integer IFORMAT1. NUMBER is then be used as a transfer destination in an assigned GO TO state ment, and IFORMAT1 is used as a format specifier. 

     The following example program shows the use of the ASSIGN statement. The program increments a real vari able 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

C234567890 

PROGRAM ASSIGN_STATEMENT 

ASSIGN 10 TO NUMBER 

ASSIGN 100 TO IFORMAT1 

10 K= K + 1 

X= X + 3. 

WRITE(6,IFORMAT1) 'FOR K= ',K,', X IS= ',X 

IF(K.LT.5) THEN 

GO TO NUMBER 

ENDIF 

100 FORMAT(1X,A,I3,A,F8.2)

 END

 

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 ex pression 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 la bels in the program unit, and integexp is an integer expres sion 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 com puted GO TO statement. 

           GO TO (10,20,30,40,50) I 

     If I= 1 during execution, control is transferred to state ment 10; if I= 2, control is transferred to statement 20; and so on. 

Typically, the GO TO and computed GO TO state ments 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 

  40 CONTINUE 

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. No tice 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 

     IF(I.EQ.1) THEN 

     X= X + 2.5

      ...

     ELSEIF(I.EQ.2) THEN 

     X= X + 4.7 

      ... 

     ELSEIF(I.EQ.3) THEN 

     X= X + 8.3 

     ... 

    ENDIF 

    END DO

     Notice the absence of GO TO statements in this exam ple. 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: 

     BACKSPACE(UNIT=5,ERR=90) 

where 90 is a statement label to which control is trans ferred in the event of an error during input/output opera tion. When no error specifier appears, the BACKSPACE statement reduces to: 

     BACKSPACE 5

 Example Program: Use of Backspace Statement 

C234567890 

            PROGRAM BACKSPACE_EXAMPLE 

            OPEN(5,FILE='X.DAT',STATUS='UNKNOWN') 

            OPEN(7,FILE='X.OUT',STATUS='UNKNOWN')

            DO J= 6,12 

            READ(5,100)X 

            X = X + J 

           WRITE(6,200) 'J= ',J,' X= ',X

           BACKSPACE 5

           END DO 

   100 FORMAT(F8.2)

   200 FORMAT(1X,A,I2,3X,A,F8.2) 

          END 

     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: 

     35.00 

     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: 

  • COMMON 
  • CHARACTER
  • DOUBLE PRECISION 
  • DATA 
  • DIMENSION 
  • INTEGER 
  • LOGICAL 
  • PARAMETER 
  • REAL 

     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 subpro gram 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: 

  • Variables A, B, and C are explicitly declared as integers in both main program and subprogram. 
  • The labeled COMMON appears in both main program and subprogram. 
  • The BLOCK DATA subprogram is named BLOCK1. ·
  • All elements of arrays A and B are being initialized, and all elements of array C are calculated. Therefore, the implied DO list in DATA and WRITE statements can be omitted.

 Example Program: Use of Block Data Subprogram 

C234567890 

C------BEGINNING OF MAIN PROGRAM 

            PROGRAM BLOCK_DATA_EXAMPLE

            INTEGER A,B,C 

            COMMON /LABEL/ A(20),B(20),C(20) 

            DO J= 1,20

            C(J)= A(J) + B(J)

            END 

            DO WRITE(6,100) C 

     100 FORMAT(1X,20I3) 

            END

C------END OF MAIN PROGRAM 

C------BEGINNING OF BLOCK DATA PROGRAM 

            BLOCK DATA BLOCK1 

            INTEGER A,B,C 

           COMMON /LABEL/ A(20),B(20),C(20) 

            DATA A /1,3,5,7,9,2,4,6,8,0,1,3,5,7,9,2,4,6,8,0/ 

            DATA B /1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0/ 

            END

C------END OF BLOCK DATA PROGRAM 

 

Things to keep in mind: 
  • In reference to the ASSIGN statement, do not perform operations with an assigned name while it has the meaning of a label. 
  • Avoid the use of the computed GO TO construct, since the combined DO loop and Block IF construct accomplishes the same objectives with more clarity and less effort. 
  • Use the BACKSPACE statement sparingly. 
  • The BLOCK DATA is a subprogram; there fore, it must end with an END statement. 
 

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: 

           EQUIVALENCE   (var1,var2,var3)

 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: 

  • A variable starting with letters I, J, K, L, M, or N is implicitly declared as integer. 
  • A variable starting with letters other than I, J, K, L, M, and N is implicitly declared as real. 

     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 im plicit 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 tem porarily suspends program execution and displays a mes sage on the terminal. It has the form:

           PAUSE display

 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 dig its. If the argument is omitted, the following message is displayed on the screen:

FORTRAN PAUSE 

     The following examples illustrate the use of PAUSE:

      PAUSE 100 

     PAUSE 'TAKE ACTION NO. 1' 

     In these cases, the following messages would be dis played on the screen, respectively: 

100 TAKE ACTION NO. 1 

     Following a pause, to resume or terminate program execution, the appropriate command would have to be en tered at the operating system level.

 Example Program: Use of Pause Statement

C234567890

             PROGRAM PAUSE_EXAMPLE 

            WRITE(6,*) 'BEFORE FIRST PAUSE' 

             PAUSE 

            WRITE(6,*) 'BEFORE SECOND PAUSE' 

            PAUSE 100 

           WRITE(6,*) 'BEFORE THIRD PAUSE' 

            PAUSE 'TAKE ACTION NO. 1' 

           WRITE(6,*) 'AFTER THREE PAUSES--THE END' 

           END

      The output from this program may look like this. 

BEFORE FIRST PAUSE 

FORTRAN PAUSE 

$ CONTINUE 

BEFORE SECOND PAUSE 

100 

$ CONTINUE 

BEFORE THIRD PAUSE 

TAKE ACTION NO. 1 

$ CONTINUE 

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:

m           REWIND(UNIT=5,ERR=95) 

where 5 is a logical unit number, and 95 is a statement la bel 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: 

          REWIND 5