HP 3000 Manuals

IF Statement (Executable) [ HP FORTRAN 77/iX Reference ] MPE/iX 5.0 Documentation


HP FORTRAN 77/iX Reference

IF Statement (Executable) 

The IF statement provides a means for decision making.  There are three
types of IF statements:

   *   Arithmetic IF
   *   Logical IF
   *   Block IF

The maximum level to which a mixture of IF and DO statements, can be
nested is 20.  Exceeding this number makes programs unnecessarily
complicated and can cause internal compiler errors stating that the
statement is too complicated.

Arithmetic IF Statement (Executable) 

An arithmetic IF statement transfers control to one of two or three
labeled statements, depending on whether an expression evaluates to a
negative, zero, or positive value.

[]
----------------------------------------------------------------------------------------------- | | | | | Item | Description/Default | Restrictions | | | | | ----------------------------------------------------------------------------------------------- | | | | | exp | Arithmetic expression. | Any type except complex, logical, | | | | or character. | | | | | ----------------------------------------------------------------------------------------------- | | | | | labeln | Label of an executable statement. | None. | | labelz | | | | labelp | | | | | | | ----------------------------------------------------------------------------------------------- When an arithmetic statement is executed, exp is evaluated. If the value is negative, control passes to the statement whose label is labeln. If the value is zero, control passes to the statement whose label is labelz. If the value is positive, control passes to the statement whose label is labelp. As an extension to the ANSI 77 standard, labelp is optional. If labelp is omitted, control transfers to labeln when the value of exp is negative or to labelz when the value of exp is zero or positive. If the value of the expression exceeds the range of the expression, an overflow condition occurs. Overflow conditions are not detected by the compiler. A hardware overflow during evaluation of any expression causes a run-time error, which halts the program. Overflow conditions can often be avoided by using logical instead of arithmetic IF statements. Two of the labels in the label list can be the same; control branches to one of two possible statements rather than three. In fact, all of the labels in the list can be the same, in which case control branches to the statement bearing the label, regardless of the results of the evaluation. If two of three labels are the same, and one of them indicates the next statement, the statement should be rewritten as a logical IF for improved readability. For example, these arithmetic IF statements: IF (exp) 10,10,20 10 ... IF (exp) 10,20,10 20 ... are the same as these logical IF statements: IF (exp .GT. 0) GOTO 20 10 ... IF (exp .NE. 0) GOTO 10 20 ... Examples Notes -------------------------------------------------------------------------------------- testa = 0. Because testa equals zero, control passes to IF (testa) 50,100,50 statement 100. i = 10 Because i + j is negative, control passes to j = -(15) statement 10. IF (i + j) 10,20,30 z = 10. Because a + z is positive, control passes to a = 60. statement 60. IF (a + z) 100,100,60 IF (a + b) 10,20,30 Control passes to statement 10, 20, or 30 depending on the value of a + b. Logical IF Statement (Executable) The logical IF statement evaluates a logical expression and executes one statement if the expression is true.
[]
----------------------------------------------------------------------------------------------- | | | | | Item | Description/Default | Restrictions | | | | | ----------------------------------------------------------------------------------------------- | | | | | exp | Logical expression. | None. | | | | | ----------------------------------------------------------------------------------------------- | | | | | statement | Executable statement. | Cannot be a DO, END, block IF, or | | | | logical IF statement. | | | | | ----------------------------------------------------------------------------------------------- The logical IF statement is a two-way decision maker. If the logical expression contained in the IF statement is true, the statement contained in the IF statement is executed and control passes to the next statement. If the logical expression is false, the statement contained in the IF statement is not executed and control passes to the next statement in the program. Examples Notes -------------------------------------------------------------------------------------- a = b Because the expression a .EQ. b is true, control IF (a .EQ. b) GOTO 100 passes to statement 100. IF (p .AND. q) res=10.5 If p and q are both true, the value of res is replaced by 10.5; otherwise the value of res is unchanged. Block IF Statement (Executable) The block IF statement block is an extension of the logical IF statement, allowing one of a set of blocks of statements to be executed, depending on the value of one or more logical expressions. The blocks within the block IF statement block are managed by four statements: * IF-THEN statement * ELSE statement * ELSE IF statement * ENDIF statement IF-THEN Statement (Executable). The IF-THEN statement evaluates a logical expression and permits the execution of two specified blocks of statements, depending on the results.
[]
----------------------------------------------------------------------------------------------- | | | | | Item | Description/Default | Restrictions | | | | | ----------------------------------------------------------------------------------------------- | | | | | exp | Logical expression. | None. | | | | | ----------------------------------------------------------------------------------------------- Semantics The IF-THEN statement, like the logical IF statement, is a two-way decision maker. If the logical expression in the IF-THEN statement evaluates to true, the block of statements between the IF-THEN statement and the next following ELSE, ELSE IF, or ENDIF statement is executed. If the logical expression in the IF-THEN statement evaluates to false, the block of statements between the corresponding ELSE or ELSE IF and ENDIF statements is executed. If no ELSE or ELSE IF block is present, control passes to the statement following the ENDIF statement. ELSE Statement (Executable). The ELSE statement terminates the block of the corresponding IF-THEN statement and marks the beginning of the ELSE block.
[]
The ELSE statement serves as a beginning marker for the block of statements to be executed if the logical expression in its corresponding IF-THEN or ELSE IF statement evaluates to false. If so, the block of statements between the ELSE and its corresponding ENDIF statement is executed. If the logical statement evaluates to true, the statements in the IF-THEN block are executed until an ELSE statement is encountered. Control then transfers to the statement following the ENDIF statement. ELSE IF Statement (Executable). The ELSE IF statement is a special case of an ELSE statement. It functions the same as an ELSE statement that has an IF-THEN statement as the first statement of its ELSE block.
[]
----------------------------------------------------------------------------------------------- | | | | | Item | Description/Default | Restrictions | | | | | ----------------------------------------------------------------------------------------------- | | | | | exp | Logical expression. | None. | | | | | ----------------------------------------------------------------------------------------------- ENDIF Statement (Executable). The ENDIF statement terminates an IF-THEN or ELSE statement block.
[]
When an IF-THEN block is terminated by an ELSE or ENDIF statement, or an ELSE block is terminated by its associated ENDIF statement, control transfers to the statement following the ENDIF statement. Nesting IF Statements. One block IF statement can contain any number of ELSE IF sub-blocks but only one ELSE sub-block. The depth to which combinations of DO loops and IF blocks can be nested is system dependent. Using ELSE IF does not change the nesting level of the IF block. (The term nesting level refers to the number of preceding IF-THEN statements minus the number of preceding ENDIF statements.) The nesting level must be equal to zero at the end of each program unit. An IF-THEN statement increases the nesting level by one, while the ENDIF statement decreases the nesting level by one. Example Nesting Level -------------------------------------------------------------------------------------- IF (exp1) THEN 0 : 1 IF (exp2) THEN : : 2 ELSE IF (exp3) THEN : : 2 ELSE : : 2 ENDIF : : 1 ELSE IF (exp4) THEN : : 1 ENDIF : 0 Examples Notes -------------------------------------------------------------------------------------- x = y Because x = y, the value of x is replaced by the IF (x.EQ.y) THEN value of x+1. Note that this is equivalent to the x=x+1 following logical IF statement: ENDIF IF (x.EQ.y) x=x+1 IF (x.LT.0) THEN If x < 0, one block of code is executed; if x >= 0, y = SQRT (ABS(x)) a different block of code is executed. z = x+1-y ELSE y = SQRT (x) z = x-1 ENDIF IF (n(i).EQ.0) THEN This example demonstrates nesting of IF blocks n (i) = n (j) using the construct: j = j+1 IF (j.LT.k) THEN IF (exp1) THEN k = k-1 : ELSE IF (j.EQ.k) THEN IF (exp2) THEN k = k+1 : ENDIF ELSE IF (exp3) THEN ELSE : m = i ENDIF k = n(i) ELSE ENDIF : ENDIF


MPE/iX 5.0 Documentation