⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fstruct.gml

📁 开放源码的编译器open watcom 1.6.0版的源代码
💻 GML
📖 第 1 页 / 共 4 页
字号:
.chap *refid=fstruct Program Structure Control Statements
.*
.if &e'&dohelp eq 0 .do begin
.section Introduction
.do end
.*
.np
The use of structured programming statements has been found to
encourage better programming and design practices among beginners, and
aids the more experienced programmer in writing error-free programs.
.np
The format of these statements and their blocks is illustrated below.
Following this, the use and meaning of each statement is described and
illustrated with examples.
In each of these illustrations, the blocks are denoted by
.mono statement(s)
and are delimited by control statements.
.np
.xt begin
In the descriptions,
.mono logical-expression
can also be an integer expression, in which case the result of the
integer expression is compared for inequality to the integer value 0.
.exam begin
    IF( LEN - 1 )THEN
.exam end
.pc
In the preceding example, the expression
.mono LEN - 1
is interpreted as
.mono LEN - 1 .NE. 0.
.xt end
.*
.section IF - ELSE - END IF
.*
.np
The
.kw ELSE
portion of this construct is optional, thus there are two possible
formats.
.mext begin
(a) IF( logical-expression )THEN    [: block-label]
        statement(s)
    END IF

(b) IF( logical-expression )THEN    [: block-label]
        statement(s)
    ELSE
        statement(s)
    END IF
.mext end
.np
This construct is an enhancement of the FORTRAN logical
.kw IF
statement.
If the value of the parenthesized logical expression is true in (a),
the block of statements following the
.kw IF
statement is executed, after which control passes to the statement
following the
.kw END IF
statement; otherwise, control will pass directly to the statement
following the
.kw END IF
statement.
When the
.kw ELSE
statement is used and the logical expression is true, the block of
statements between the
.kw IF
and the
.kw ELSE
statements is executed and then control passes to the statement
following the
.kw END IF
statement; otherwise the block of statements following
.kw ELSE
statement is executed and then control passes to the statement
following the
.kw END IF
statement.
.np
.xt begin
An optional block label may be specified with the
.kw IF
statement (see the
.kw CYCLE,
.kw EXIT
or
.kw QUIT
statement for more information).
.xt end
.np
Examples follow which illustrate the use of the two formats.
.keep 10
.exam begin
      IF( I .EQ. 0 )THEN
          PRINT *, 'I IS ZERO'
          I = 1
      END IF
.exam end
.np
If variable
.id I
is zero when the
.kw IF
statement is executed, the string
.mono I IS ZERO
will be printed, variable
.id I
will be assigned the value 1, and the statement following the
.kw END IF
will be executed.
If variable
.id I
is not zero when the
.kw IF
statement is executed, control will pass to the statement following
the
.kw END IF
statement.
.keep 10
.exam begin
      IF( A .GT. B )THEN
          PRINT *, 'A GREATER THAN B'
          A = A - B
      ELSE
          PRINT *, 'A NOT GREATER THAN B'
      END IF
.exam end
.np
If the value of variable
.id A
is greater than the value of variable
.id B
when this
.kw IF
statement is executed, the string
.mono A GREATER THAN B
will be printed and variable
.id A
will be assigned the value of the expression
.mono A - B.
Control will then pass to the statement following the
.kw END IF
statement.
.np
If the value of variable
.id A
is not greater than the value of
variable
.id B
when the
.kw IF
statement is executed, the string
.mono A NOT GREATER THAN B
will be printed and control will pass to the statement following the
.kw END IF
statement.
.*
.keep 19
.section ELSE IF
.*
.np
A further enhancement of the IF-THEN-ELSE construct is the
.kw ELSE IF
statement which may be used in the following two formats:
.mext begin
(a) IF( logical-expression-1 )THEN    [: block-label]
        statement(s)
    ELSE IF( logical-expression-2 )THEN
        statement(s)
    ...
    END IF

(b) IF( logical-expression-1 )THEN    [: block-label]
        statement(s)
    ELSE IF( logical-expression-2 )THEN
        statement(s)
    ...
    ELSE
        statement(s)
    END IF
.mext end
.np
The presence of the "..." in the above formats indicates that the
.kw ELSE IF
statement may be repeated as often as desired.
If the value of
.id logical-expression-1
is true in case (a), the block of statements following the
.kw IF
statement up to the first
.kw ELSE IF
statement is executed, after which control passes to the statement
following the
.kw END IF
statement; otherwise, control will pass to the first
.kw ELSE IF
statement.
If the value of
.id logical-expression-2
is true, the block of statements following the first
.kw ELSE IF
statement up to the next
.kw ELSE IF
statement or
.kw END IF
statement is executed, after which control passes to the statement
following the
.kw END IF
statement; otherwise, control will pass to the next
.kw ELSE IF
statement, if there is one, or directly to the statement following the
.kw END IF
statement.
When the
.kw ELSE
statement is used, as in case (b), and the values of all the logical
expressions in the
.kw IF
and
.kw ELSE IF
statements are false, the block of statements following the
.kw ELSE
statement is executed and then control passes to the statement
following the
.kw END IF
statement.
.xt on
An optional block label may be specified with the
.kw IF
statement (see the
.kw CYCLE,
.kw EXIT
or
.kw QUIT
statement for more information).
.xt off
.np
Examples follow which illustrate the use of the two formats.
.keep 10
.exam begin
      IF( I .EQ. 0 )THEN
          PRINT *, 'I IS ZERO'
      ELSE IF( I .GT. 0 )THEN
          PRINT *, 'I IS GREATER THAN ZERO'
      END IF
.exam end
.np
If variable
.id I
is zero when the
.kw IF
statement is executed, the string
.mono I IS ZERO
will be printed and the statement following the
.kw END IF
statement will be executed.
If variable
.id I
is not zero when the
.kw IF
statement is executed, control will pass to the
.kw ELSE IF
statement.
If variable
.id I
is greater than zero, the string
.mono I IS GREATER THAN ZERO
will be printed and the statement following the
.kw END IF
statement will be executed.
If variable
.id I
is less than zero then nothing would be printed and control passes
from the
.kw ELSE IF
statement to the statement following the
.kw END IF
statement.
.keep 10
.exam begin
      IF( A .GT. B )THEN
          PRINT *, 'A GREATER THAN B'
          A = A - B
      ELSE IF( A .LT. B )THEN
          PRINT *, 'A LESS THAN B'
          A = B - A
      ELSE
          PRINT *, 'A EQUAL TO B'
          A = 0.0
      END IF
.exam end
.np
If the value of variable
.id A
is greater than the value of variable
.id B
when the
.kw IF
statement is executed, the string
.mono A GREATER THAN B
will be printed and variable
.id A
will be assigned the value of the expression
.mono A - B.
Control will then pass to the statement following the
.kw END IF
statement.
.np
If the value of variable
.id A
is not greater than the value of variable
.id B
when the
.kw IF
statement is executed, control passes to the
.kw ELSE IF
statement.
If the value of variable
.id A
is less than the value of variable
.id B,
the string
.mono A LESS THAN B
will be printed and variable
.id A
will be assigned the value of the expression
.mono B - A.
Control will then pass to the statement following the
.kw END IF
statement.
.np
If the value of variable
.id A
is not less than the value of variable
.id B
when the
.kw ELSE IF
statement is executed, the string
.mono A EQUAL TO B
will be printed and variable
.id A
will be assigned the value zero.
Control will pass to the statement following the
.kw END IF
statement.
.*
.keep 10
.section DO - END DO
.*
.mext begin
  DO init-expr,end-value[,inc-value] [: block-label]
      statement(s)
  END DO
.mext end
.np
This extension to FORTRAN 77 allows the creation of DO-loops without the
introduction of statement numbers.
An optional block label may be specified (see the
.kw CYCLE,
.kw EXIT
or
.kw QUIT
statement for more information).
The
.kw END DO
statement is used to indicate the end of the range of its
corresponding
.kw DO
statement.
A statement number may not be specified in the corresponding
.kw DO
statement.
Nested DO-loops of this form require separate
.kw END DO
statements to terminate the range of the corresponding
.kw DO
statement.
Since a statement number may appear on the
.kw END DO
statement, the number may be used to terminate outer DO-loops.
This is not a recommended practice (a
.kw CONTINUE
statement or a structured
.kw DO
statement should be used).
A transfer of control from within the DO-loop to a statement number on
the
.kw END DO
statement is treated in the same manner as if the word
.kw CONTINUE
had been used instead of
.kw END DO.
.np
Some examples follow.
.keep 10
.exam begin
      DO I = 1, 3
          DO J = 1, 5
              PRINT *, MATRIX( I, J )
          END DO
      END DO
.exam end
.np
The above is equivalent to the following example which uses statement
numbers.
.keep 10
.exam begin
      DO 10 I = 1, 3
          DO 20 J = 1, 5
              PRINT *, MATRIX( I, J )
20        CONTINUE
10    CONTINUE
.exam end
.np
The next example demonstrates the use of a
.kw GO TO
statement to control execution of all or part of a DO-loop.
.keep 10
.exam begin
      DO I = 1, 3
          DO J = 1, 5
              PRINT *, 'INNER LOOP - J=', J
              IF( J .LE. 3 )GO TO 20
              PRINT *, 'J > 3'
20        END DO
          PRINT *, 'OUTER LOOP - J=', J
      END DO
.exam end
.np
A result of this example is that the character string
.mono J > 3
is printed 6 times (i.e., twice for each iteration of the outer loop).
Of course there is a much better way of coding this algorithm using
the IF-END IF construct.
The example is included to illustrate the behaviour of transfers of
control to an
.kw END DO
statement.
The following example is an equivalent algorithm to the one above but
the intent is much clearer.
.keep 10
.exam begin
      DO I = 1, 3
          DO J = 1, 5
              PRINT *, 'INNER LOOP - J=', J
              IF( J .GT. 3 )THEN
                  PRINT *, 'J > 3'
              END IF
          END DO
          PRINT *, 'OUTER LOOP - J=', J
      END DO
.exam end
.*
.keep 10
.section DO WHILE - END DO
.*
.mext begin
  DO WHILE (e) [: block-label]
      statement(s)
  END DO
.mext end
.np
This extension to FORTRAN 77 allows the creation of DO-loops without
iterative techniques.
Instead, the DO-loop is executed while the parenthesized expression is
true.
The logical expression is evaluated before entry to the DO-loop.
If the value is false, control is transferred to the statement following
the
.kw END DO
statement.
If the logical expression if true, the statements of the DO-loop are
executed.
When the
.kw END DO
statement is reached, the expression is re-evaluated and program control
proceeds as previously described.
An optional block label may be specified (see the
.kw CYCLE,
.kw EXIT
or
.kw QUIT
statement for more information).
.np
An optional statement number can be specified after the
.kw DO
keyword.
When the
.kw END DO
statement is used to indicate the end of the range of its
corresponding
.kw DO WHILE
statement, a statement number may not be specified.
.np
Some examples follow.
.keep 10

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -