📄 fstruct.gml
字号:
.kw AT END
statement but may also be used to cause an unconditional transfer of
control.
(The
.kw AT END
statement is described in a subsequent section).
.np
Examples of the
.kw QUIT
statement with and without a block label follow.
.keep 10
.exam begin
CHARACTER CH
READ *, CH
GUESS
IF( CH .LT. 'a' )QUIT
IF( CH .GT. 'z' )QUIT
PRINT *, 'Lower case letter'
ADMIT
IF( CH .LT. 'A' )QUIT
IF( CH .GT. 'Z' )QUIT
PRINT *, 'Upper case letter'
ADMIT
IF( CH .LT. '0' )QUIT
IF( CH .GT. '9' )QUIT
PRINT *, 'Digit'
ADMIT
PRINT *, 'Special character'
END GUESS
END
.exam end
.np
The above statements read and print values until an end of file
occurs.
At that point control is passed to the
.kw QUIT
statement, as specified by the
.kw AT END
statement.
The
.kw QUIT
statement causes control to continue with the statement after the
.kw END LOOP
statement.
.keep 15
.exam begin
CHARACTER RECORD(80)
LOOP : RDREC
READ(5,100) RECORD
AT END, STOP
DO I = 1, 80
IF( RECORD(I) .LT. '0'
+ .OR. RECORD(I) .GT. '9' )QUIT : RDREC
END DO
WRITE(6,101) RECORD
END LOOP
PRINT *, 'INVALID RECORD'
.exam end
.np
The above example reads in records and verifies that they contain
only numeric data.
The
.kw QUIT
statement is within two levels of nesting: the DO-loop and the
LOOP-END LOOP structure.
If a non-numeric character is found, the
.mono QUIT : RDREC
statement will cause control to be passed to the
.kw PRINT
statement after the
.kw END LOOP
statement.
.*
.keep 10
.section EXIT
.*
.mext begin
EXIT [ : block-label]
.mext end
.np
The
.kw EXIT
statement is used to transfer control:
.autopoint
.point
from within a loop (DO, DO WHILE, WHILE or LOOP) to the
statement following the loop,
.point
from within a GUESS or ADMIT block to the statement following
the
.kw ENDGUESS
statement, or
.point
from within a remote block to the statement following the
.kw EXECUTE
statement that invoked the remote block.
.endpoint
.np
When transferring out of a loop, control is passed to the statement
following the
.kw END DO,
.kw END WHILE,
.kw END LOOP
or
.kw UNTIL
statement.
.np
When transferring out of a GUESS block, control is passed to the
statement after the corresponding
.kw END GUESS
statement.
.np
When transferring out of a REMOTE-block, control passes to the
statement following the
.kw EXECUTE
statement that invoked the REMOTE-block.
.np
If no block label is specified in the
.kw EXIT
statement, control is transferred from the immediately enclosing
structure.
If several structures or DO-loops are nested, it is possible to exit
from any one of them by specifying the block label of the
corresponding block structure.
.np
The
.kw EXIT
statement is most commonly used as the statement in a logical
.kw IF
or
.kw AT END
statement but may also be used to cause an unconditional transfer of
control.
(The
.kw AT END
statement is described in a subsequent section).
.np
Examples of the
.kw EXIT
statement with and without a block label follow.
.keep 10
.exam begin
LOOP
READ *, X
AT END, EXIT
PRINT *, X
END LOOP
.exam end
.np
The above statements read and print values until an end of file
occurs.
At that point control is passed to the
.kw EXIT
statement, as specified by the
.kw AT END
statement.
The
.kw EXIT
statement causes control to continue with the statement after the
.kw END LOOP
statement.
.keep 15
.exam begin
CHARACTER RECORD(80)
LOOP : RDREC
READ(5,100) RECORD
AT END, STOP
DO I = 1, 80
IF( RECORD(I) .LT. '0'
+ .OR. RECORD(I) .GT. '9' )EXIT : RDREC
END DO
WRITE(6,101) RECORD
END LOOP
PRINT *, 'INVALID RECORD'
.exam end
.np
The above example reads in records and verifies that they contain
only numeric data.
The
.kw EXIT
statement is within two levels of nesting: the DO-loop and the
LOOP-END LOOP structure.
If a non-numeric character is found, the
.mono EXIT : RDREC
statement will cause control to be passed to the
.kw PRINT
statement after the
.kw END LOOP
statement.
.*
.keep 10
.section CYCLE
.*
.mext begin
CYCLE [ : block-label]
.mext end
.np
The
.kw CYCLE
statement is used to cause a transfer of control from within a loop to
the terminal statement of a corresponding
.kw DO,
.kw DO WHILE,
.kw WHILE
or
.kw LOOP
statement.
If
.id block-label
is present then control is transferred to the terminal statement of
the block identified by that block label.
.np
If no block label is specified in the
.kw CYCLE
statement, control is transferred to the terminal statement of the
immediately enclosing loop structure.
If several loop structures are nested, it is possible to cycle to the
terminal statement of any one of them by specifying the block label of
the corresponding block structure.
.np
The
.kw CYCLE
statement is most commonly used as the statement in a logical
.kw IF
statement but may also be used to cause an unconditional transfer of
control.
.np
Examples of the
.kw CYCLE
statement with and without a block label follow.
.keep 10
.exam begin
LOOP
WRITE( UNIT=*, FMT='(A)' ) 'Enter a number'
READ( UNIT=*, FMT='(F10.4)', IOSTAT=IOS ) X
IF( IOS .NE. 0 ) CYCLE
IF( X .LT. 0 ) EXIT
PRINT *, X, SQRT( X )
END LOOP
.exam end
.np
The above statements read and print values until a negative integer
value is entered.
If an input error occurs, the input operation (READ) is retried using
the
.kw CYCLE
statement.
The
.kw CYCLE
statement causes control to resume at the
.kw END LOOP
statement which then immediately transfers control to the statement
following the
.kw LOOP
statement.
.keep 15
.exam begin
CHARACTER RECORD(80)
LOOP : RDREC
READ(5,100) RECORD
AT END, STOP
DO I = 1, 80
IF( RECORD(I) .LT. '0'
+ .OR. RECORD(I) .GT. '9' )THEN
PRINT *, 'INVALID RECORD'
CYCLE : RDREC
ENDIF
END DO
WRITE(6,101) RECORD
END LOOP
.exam end
.np
The above example reads in records and verifies that they contain only
numeric data.
If the record does not, the input operation is tried again.
The
.kw CYCLE
statement is within three levels of nesting: the IF, the DO-loop, and
the LOOP-END LOOP structure.
If a non-numeric character is found, the
.mono CYCLE : RDREC
statement will cause control to be passed to the
.kw READ
statement that follows the
.kw LOOP
statement.
.*
.keep 10
.section AT END
.*
.mext begin
(READ statement)
AT END DO [: block-label ]
statement(s)
END AT END
.mext end
.pc
or
.mext begin
(READ statement)
AT END, statement
.mext end
.np
The
.kw AT END
control statement, an extension to FORTRAN 77, is an extension of the
.kw END=
option of the FORTRAN
.kw READ
statement for sequential files.
It allows a statement or a block of code following the
.kw READ
statement to be executed when an end of file condition is encountered
during the
.kw READ
and to be by-passed immediately following a
.kw READ
statement.
It is not valid to use this control statement with direct-access or
memory-to-memory reads.
Clearly, it is not valid to use this statement when
.kw END=
is specified in the
.kw READ
statement.
.keep 10
.exam begin
READ(7, *) I, X
AT END DO
PRINT *, 'END-OF-FILE ENCOUNTERED'
EOFSW = .TRUE.
END AT END
.exam end
.np
If the
.kw READ
statement is executed without encountering end of file, control passes
to the statement following the
.kw END AT END
statement.
If an end of file condition occurs during the read, the string,
.mono END-OF-FILE ENCOUNTERED
is printed, logical variable
.id EOFSW
is assigned the value
.mono .TRUE.
and control passes to the statement following the
.kw END AT END
statement.
.keep 10
.exam begin
READ(7, *) X
AT END, EOFSW = .TRUE.
.exam end
.np
If an end of file is not encountered by the
.kw READ
statement, control passes to the statement following the
.kw AT END
statement.
If an end-of-file condition occurs, variable
.id EOFSW
is set to
.mono .TRUE.
and control then passes to the statement following the
.kw AT END
statement.
Note that the use of the second form of the
.kw AT END
statement requires the use of a comma (,) between the
.mono AT END
word and the executable statement.
This is necessary to distinguish the case where the executable
statement is an assignment statement.
The executable statement may be any statement that is also allowed as
the operand of a logical
.kw IF
statement.
.*
.section Notes on Structured Programming Statements
.*
.np
In addition to the definitions and examples of these constructs, the
following points should be noted:
.begpoint
.point (i)
Any of the new control statements with their blocks may be used within
the block of any other statement.
For example, a WHILE-block may contain another WHILE or an
IF-THEN-ELSE block.
Blocks may be nested in this manner to any level within storage
limitations.
An important exception to this rule is the REMOTE-block A REMOTE-block
may contain other types of blocks (nested to any level); however,
another REMOTE-block may not be defined within it.
Furthermore, REMOTE-blocks may not be defined within another control
structure.
The following example is illegal.
.keep 10
.exam begin
* Illegal definition of a REMOTE-block.
IF( I .EQ. 3 )then
REMOTE BLOCK A
.
.
.
END BLOCK
END IF
.exam end
.point (ii)
When nesting blocks, the inner blocks must always be completed with
the appropriate block-terminating
.kw END
statement before the outer blocks are terminated.
Similarly, when nesting blocks with DO-loops, a DO-loop started within
a block must be completed before the block is completed.
A block started within a DO-loop must be terminated before the DO-loop
is completed.
Indenting the statements of each new block, as shown in the examples,
is helpful in avoiding invalid nesting and helps to make the structure
of the program visually obvious.
.point (iii)
The normal flow of control of the new programming constructs described
earlier may be altered with standard FORTRAN control statements.
For example, the program may exit from a block using a
.kw GO TO,
.kw STOP,
.kw RETURN
or arithmetic
.kw IF
statement.
However, a block may not be entered in the middle through use of any
control statement such as
.kw GO TO
or the arithmetic
.kw IF.
.np
Consider the following example.
.keep 10
.exam begin
GO TO 20
10 IF( X .GT. Y )THEN
CALL REDUCE( X, Y )
20 X = X - 1
ELSE
CALL SCALE( X )
END IF
.exam end
.pc
This is an example of an illegal attempt to transfer execution into
the middle of an IF-block.
The statement
.mono X = X - 1
is contained within the IF-block and may only be transferred to from
within the block.
.keep 10
.exam begin
IF( X .GT. Y )THEN
20 CALL REDUCE( X, Y )
X = X - 1
IF( X .GT. 0 )GO TO 20
ELSE
CALL SCALE( X )
END IF
.exam end
.pc
This last example demonstrates a legal transfer of control within an
IF-block.
However, we have seen better ways to express the loop with this
IF-block.
.keep 10
.exam begin
IF( X .GT. Y )THEN
LOOP
CALL REDUCE( X, Y )
X = X - 1
UNTIL( X .LE. 0 )
ELSE
CALL SCALE( X )
END IF
.exam end
.point (iv)
Many control structure statements cannot be branched to using a
.kw GO TO
statement.
For a list of these statements,
see the section entitled :HDREF refid='stmtcls'. in the chapter
entitled :HDREF refid='fstats' page=no..
.point (v)
Many control structure statements cannot be the object statement of a
logical
.kw IF
statement, or be the last statement of a DO-loop.
For a list of these statements,
see the section entitled :HDREF refid='stmtcls'. in the chapter
entitled :HDREF refid='fstats' page=no..
.endpoint
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -