Conditional statements, in particular the block IF, are the workhorses of the Fortran language. They are executable statements, used to conditionally execute:
The conditional execution of a block is accomplished with the block IF. The conditional execution of a statement is accomplished with the logical IF. The conditional transfer of control is accomplished with the arithmetic IF. The block IF is by far the most commonly used IF statement. The logical IF has limited application, although it remains in use. The arithmetic IF is a remnant of older versions of the language, and it has been categorized as obsolescent by the Fortran 90 standard (see Section 1.4). Accordingly, its use is discouraged in the development of new programs. A block is a set of zero or more executable statements that is treated as a unit. A null block has zero statements. Execution of a null block has no effect. A block IF construct consists of one or more blocks separated by the partial statements IF (exp) THEN, ELSE IF (exp) THEN, ELSE, and END IF. The word exp enclosed in parentheses stands for a logical expression. A block IF construct always begins with the partial statement IF (exp) THEN and ends with the partial statement END IF. A block forming part of a block IF construct is conditionally executed based on the satisfaction of the logical expression exp contained in the partial statement statement IF (exp) THEN or ELSEIF (exp) THEN immediately preceding it. Example of Block IF Construct Sequence IF (exp1) THEN ... ! Block 1 ELSE IF (exp2) THEN ... ! Block 2 ELSE IF (exp3) THEN ... ! Block 3 ELSE IF (exp4) THEN Block 4 ELSE ... ! Block 5 ENDIF
4.1 LOGICAL EXPRESSIONS
A logical expression is one that can take a value of true (.TRUE.) or false (.FALSE.). Logical expressions link two variables with relational and/or logical operators. Table 4.1 shows a list of relational operators. Table 4.2 shows a list of logical operators.
Note: Relational operators are shown in upper case, enclosed by delimiting periods.
Note: Logical operators are shown in upper case, enclosed by delimiting periods. A relational expression is a special type of logical expression consisting of two arithmetic expressions connected by a relational operator, for example: X.GT.Y where X and Y are arithmetic expressions and GT is a relational operator. The delimiting periods are required. During execution, the relational expression is evaluated. If X is greater than Y, the relational expression is assigned the value .TRUE.; otherwise, it is .FALSE. Complex variables can be related only with the .EQ. and .NE. relational operators (see Table 4.1). Furthermore, two complex values are equal only if their corresponding real and imaginary parts are equal. The following are additional examples of relational expressions: Y.GE.Z A+B.LT.C*D X+Y.LE.P/Q JTIME.EQ.KTIME APPLE.NE.ORANGE All variables included in these examples are of numeric data type (integer, real, complex, or double precision).
A logical expression can contain any combination of relational and/or logical operators (including the case of no operator), as shown in the following examples: A A.AND.B C.GT.D.AND.X.LE.Y A and B are logical expressions; C, D, X, and Y are arithmetic expressions. The delimiting periods are required. During execution, the logical expression is evaluated. In the first example, if A is true, the logical expression is true; otherwise, it is false. In the second example, if both A and B are true, the logical expression is true; otherwise, it is false. In the third example, if C is greater than D, and X is less than or equal to Y, the logical expression is true; otherwise, it is false. Logical operations (.AND., .OR., and so on) can only be performed on logical data types (accepting only .TRUE. or .FALSE.). The following are examples of logical expressions: E.OR.F E.NEQV.F G.EQV.H .NOT.R U.AND..NOT.V During execution, these logical expressions are evaluated as follows:
In the last example, note that each one of the logical operators AND and NOT is delimited by periods. Also, note that only the operator .NOT. can immediately follow any of the other logical operators. An example of a logical expression that includes a relational expression is : (X.GE.45..AND.A.NEQV.B) The combined expression is true if the relational expression X.GE.45. and the logical expression A.NEQV.B are both true. The logical and relational expressions introduced in this section are placed within parentheses in the block IF statement (exp). 4.2 BLOCK IF CONSTRUCT
A block IF construct consists of one or more blocks separated by the partial statements
in that specific order. Therefore, a block IF construct always begins with the partial statement IF (exp) THEN, and ends with the partial statement END IF. The partial statement ELSE IF (exp) THEN can be repeated zero or more times. The partial statement ELSE is optional; if included, it can only appear once in a given block IF construct (excluding nesting--see Nested Block IF Constructs later in this section). In general, at most one of the blocks contained within the block IF construct is executed. However, if the construct includes the optional ELSE statement, exactly one of the blocks is executed. The logical expressions (exp) are evaluated in sequential order until either
In any of these two cases, the block immediately following is executed and this completes the execution of the entire block IF construct. In the first case, remaining ELSE IF (exp) THEN statements, if any, are not evaluated. If no true value is found and no ELSE statement is encountered, the execution of the block IF construct is completed with out the execution of any block. The following are examples of block IF constructs: Example 1 IF (exp1) THEN ... !Block 1 ELSE IF (exp2) THEN ... ! Block 2 END IF Example 2 IF (exp1) THEN ... ! Block 1 ELSE IF (exp2) THEN ... ! Block 2 ELSE ... ! Block 3 END IF In these examples, the words exp1 and exp2 enclosed in parentheses stand for logical expressions. The symbol ... followed by an appropriate trailing comment stands for a given block, i.e., a set of executable statements that is treated as a unit. The following is an example of a block IF construct containing a block: IF (A.GT.0.) THEN B= SQRT(A) ! These two statements C= A + B ! form a block END IF The following rules apply for block IF contructs:
The following examples further illustrate the block IF construct. Example 1 IF(K.EQ.L) THEN ... ! Block 10 END IF If If the relational expression (K.EQ.L) is true, block 10 is executed; otherwise, block 10 is bypassed. Example 2 IF (X.LE.150.) THEN ... ! Block 20 ELSE IF (X.LE.250.) THEN ... ! Block 21 END IF If the relational expression (X.LE.150.) is true, block 20 is executed; otherwise, if the expression (X.LE.250.) is true, block 21 is executed; otherwise, all blocks are by passed. At most one (1) block is executed per block IF construct. Example 3 IF (X.LE.150.) THEN ... ! Block 30 ELSE IF (X.LE.250.) THEN ... ! Block 31 ELSE IF (X.LE.350.) THEN ... ! Block 32 END IF If the relational expression (X.LE.150.) is true, block 30 is executed; otherwise, if the expression (X.LE.250.) is true, block 31 is executed; otherwise, if the expression (X.LE.350.) is true, block 32 is executed; otherwise, all blocks are bypassed. At most one (1) block is executed per block IF construct. Note that once an expression is found to be true, the rest are not evaluated. Example 4 IF (X.LE.XLIMIT) THEN ... ! Block 40 ELSE ... ! Block 41 END IF If the relational expression (X.LE.XLIMIT) is true, block 40 is executed; otherwise, block 41 is unconditionally executed due to the presence of the ELSE statement. Example 5 IF (X.LE.375.) THEN ... ! Block 50 ELSE IF(X.LE.625.) THEN ... ! Block 51 ELSE ... ! Block 52 END IF If the expression (X.LE.375.) is true, block 50 is exe cuted; otherwise, if the expression (X.LE.625.) is true, block 51 is executed. If neither of the above are found to be true, block 52 is unconditionally executed. Example 6 IF (X.LE.375.) THEN ... ! Block 60 ELSE IF (X.LE.625.) THEN ... ! Block 61 ELSE IF (X.LE.900.) THEN ... ! Block 62 ELSE ... ! Block 63 END IF If the expression (X.LE.375.) is true, block 60 is executed; otherwise, if the expression (X.LE.625.) is true, block 61 is executed; otherwise, if the expression (X.LE.900.) is true, block 62 is executed. If none of the above are found to be true, block 63 is unconditionally executed.
Nested Block IF Constructs It is possible to nest two or more block IF constructs, i.e., to insert one block IF construct inside another block IF construct. However, the nested block IF should be completely contained within one block of the nesting block IF. That is, the nested block IF should not overlap blocks of the nesting block IF. There is no limit to the number of nestings allowed in block IF constructs, provided the no-overlap rule is strictly adhered to. The following examples illustrate the use of nested block IF constructs. Example 1 IF (A.LE.3.5) THEN ... ! Block 10 ELSE IF (A.LE.5.5) THEN ... ! Block 20 IF (K.EQ.1) THEN !Nested block ... ! Block 21 END IF ! Nested block ELSE ... ! Block 30 END IF If the relational expression (A.LE.3.5) is true, block 10 is executed; otherwise, if the expression (A.LE.5.5) is true, block 20 and the nested block IF that follows it (indented here only for clarity), including block 21, are executed. If neither (A.LE.3.5) nor (A.LE.5.5) is true, block 30 is un conditionally executed. It is good programming practice to label nested block IF constructs with trailing comments to help identify nested from nesting block IF constructs. Following this practice, the preceding example would look like this: Example 2 IF (A.LE.3.5) THEN ! 100 ... ! Block 10 ELSE IF (A.LE.5.5) THEN ! 101 ... ! Block 20 IF (K.EQ.1) THEN ! 200 ... ! Block 21 END IF ! 201 ELSE ! 102 ... ! Block 30 END IF ! 103 It is seen that the 100 series numeric trailing comments mark the nesting (outer) block IF. Likewise, the 200 series mark the nested (inner) block IF construct.
4.3 LOGICAL IF STATEMENT
Unlike the block IF construct, which conditionally executes a block, the logical IF statement conditionally executes a single statement. It takes the form IF (exp) execstat where exp is a logical expression and execstat is any executable statement, excluding IF and DO (see Chapter 7). During execution, if the logical expression exp within the parentheses is true, the statement execstat is executed; otherwise, the statement execstat is bypassed. The following are examples of logical IF statements: IF (B) X= Y + Z IF (C.GT.4.) STOP IF (D.AND.E) IFLAG= 1 IF (F.EQV.G) KOUNT= KOUNT + 1 Note the following:
A logical IF can be readily converted to a block IF. For instance, the statement IF (C.GT.4.) STOP can alternatively be written in the following form: IF (C.GT.4.) THEN STOP ENDIF While the block IF construct form is longer, it is usually preferred because it leads to a more readable program. Too many logical IF statements result in a disruptive and difficult-to-read program. Therefore, it is good programming practice to avoid the use of the logical IF statement. 4.4 GO TO AND CONTINUE STATEMENTS
The GO TO statement is used to transfer control to another statement located elsewhere in the same program unit. It has the form GO TO statnum where statnum is a valid statement label in the same program unit as the GO TO statement. Execution of a GO TO statement causes the statement identified by the label to be executed next. For instance, execution of the statement GO TO 20 causes statement 20 to be executed next. The GO TO statement can appear by itself, or forming an intrinsic part of a logical IF, as follows: IF(N.EQ.L) GO TO 40
In this example, if the relational expression N.EQ.L is true, control branches to statement 40. It is good programming practice to use the GO TO statement sparingly, only when absolutely necessary. In addition, the logical IF/GO TO combination is highly disruptive, and its use should be minimized. Often it is better to use a block IF construct instead of a series of logical IF/GO TO statements. CONTINUE Statement Execution of a CONTINUE statement has no effect. The CONTINUE statement is usually placed in between two executable statements or groups of executable statements. It has the form: CONTINUE Upon execution of this statement, control transfers to the statement immediately following. When preceded by an appropriate statement label, the CONTINUE statement can serve as the destination of a logical IF/GO TO statement or as the last statement of a DO loop (see Chapter 7). For example: IF (N.EQ.L) GO TO 50 WRITE (6,*) X,Y,Z 50 CONTINUE If the relational expression N.EQ.L is true, control branches to statement 50, and the statement WRITE(6,*) X,Y,Z is effectively bypassed. GO TO-CONTINUE Loop The GO TO and CONTINUE statements can be used to set up a loop construct to execute a block more than once. The loop works by transfering control from the bottom of the block (using a GO TO statement) to the top of the block (usually a CONTINUE statement). Thus, the execution of the block is repeated as many times as needed (See examples below). Other more advanced types of loop constructs are accomplished with the DO and DO WHILE statements (Chapter 7). An example of the GO TO-CONTINUE loop is: DATA N /0/ 10 CONTINUE ! Top of loop N= N +1 READ(5,*) A, B X= A*B WRITE(6,*) X IF (N.EQ.8) STOP GO TO 10 ! Bottom of loop This example initializes a counter N with the value 0, and sets up a GO TO-CONTINUE loop to read two variables A and B, calculate X= A*B, and write X. Each time control goes through the loop, the counter N is incremented by 1. Execution stops when N = 8, i.e., after completion of 8 passes through the loop. The logical IF statement is used to terminate execution (with the STOP statement, as in the previous example) or to exit the loop when appropriate (with a GO TO statement, as in the next example). Without such loop stop or exit, the loop would continue to be executed indefinitely, which is not desirable. A loop that executes indefinitely is referred to as an infinite loop. Identifying and fixing an infinite loop is part of normal debugging in source program development. An example of a loop exit via a GO TO is: DATA N /0/ 10 CONTINUE ! Top of loop N= N +1 READ(5,*) A, B X= A*B WRITE(6,*) X IF (N.EQ.8) GO TO 20 ! Loop exit GO TO 10 ! Bottom of loop 20 CONTINUE An example of an infinite loop is: DATA N /0/ 10 CONTINUE ! Top of loop N= N +1 READ(5,*) A, B X= A*B WRITE(6,*) X GO TO 10 ! Bottom of loop Note that this loop would continue to execute indefinitely, with nothing to stop it. This infinite loop would tie up the processor, effectively distracting it from completing other useful tasks.
4.5 ARITHMETIC IF STATEMENT
Unlike the logical IF/GO TO combination, which conditionally transfers control to a statement located elsewhere in the program unit, the arithmetic IF statement transfers control to at least one and at most three statements. The arithmetic IF statement has the form IF (numexp) label1, label2, label3 where numexp is a numeric expression, and label1, label2, and label3 are the first, second, and third labels, respectively. Each of these must be a valid statement label in the same program unit as the given arithmetic IF statement. Execution of an arithmetic IF statement causes the numeric expression numexp to be evaluated, followed by a transfer of control:
The same label may appear more than once in one arithmetic IF statement. The following are two examples of arithmetic IF statements: IF (A) 10,20,30 IF (L-M) 40,40,50 In the first example, if A is less than 0., control branches to statement 10. If A is equal to 0., control branches to statement 20. If A is greater than 0., control branches to statement 30. In the second example, if L-M is less than or equal to 0, control branches to statement 40. If L-M is greater than 0, control branches to statement. Note that the arithmetic IF statement has been declared as obsolescent by the Fortran 90 standard. Therefore, its use should be avoided in new program development, and is described here only for completeness. 4.6 EXAMPLE PROGRAMS
Example Program 1: Logical Expressions
This example accomplishes the following tasks:
Note that this is only an example. In a real-world situation, some or all of the variables would be read as input rather than be initialized with a DATA statement. Also, note that some processors may implicitly initialize all logical variables as .FALSE., making such initialization redundant. Example Program 2: Nested Block IF Construct
This example accomplishes the following tasks:
Again, this is only an example. In a real-world situation, some or all of the variables would be read as input rather than be initialized with a DATA statement. Also, the presence of numeric trailing comments helps identify the nested block (200 series) from the nesting block IF construct (100 series). Note that every block IF construct finishes with its own END IF (partial) statement, and every program finishes with it own END statement. 4.7 SUMMARY
The conditional IF statements are used to conditionally execute
PROBLEMS
|
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. |