IF - Arithmetic

Statement: Conditionally transfers control to one of three statements, based on the value of an arithmetic expression. It is an obsolescent feature in Fortran 95 and Fortran 90.

Syntax

IF (expr) label1, label2, label3

expr
Is a scalar numeric expression of type integer or real (enclosed in parentheses).

label1, label2, label3
Are the labels of valid branch target statements that are in the same scoping unit as the arithmetic IF statement.

Rules and Behavior

All three labels are required, but they do not need to refer to three different statements. The same label can appear more than once in the same arithmetic IF statement.

During execution, the expression is evaluated first. Depending on the value of the expression, control is then transferred as follows:

If the Value of expr is:  Control Transfers To: 
Less than 0  Statement label1 
Equal to 0  Statement label2 
Greater than 0  Statement label3 

Compatibility

CONSOLE STANDARD GRAPHICS QUICKWIN GRAPHICS WINDOWS DLL LIB

See Also: SELECT CASE...END SELECT, Execution Control, Obsolescent Features in Fortran 90

Examples

The following example transfers control to statement 50 if the real variable THETA is less than or equal to the real variable CHI. Control passes to statement 100 only if THETA is greater than CHI.

  IF (THETA-CHI) 50,50,100

The following example transfers control to statement 40 if the value of the integer variable NUMBER is even. It transfers control to statement 20 if the value is odd.

  IF (NUMBER / 2*2 - NUMBER) 20,40,20

The following statement transfers control to statement 10 for n < 10, to statement 20 for n = 10, and to statement 30 for n > 10:

  IF (n-10) 10, 20, 30

The following statement transfers control to statement 10 if n<= 10, and to statement 30 for n>10:

  IF (n-10) 10, 10, 30