Conditions in FORTRAN IV

 


FORTRAN IV: pre-dates structured languages (i.e., no blocks), but a step above assembly language.

Example (in C):

        /* compute diff = c - max(a, b) */
	if ( a > b )
	   max = a;
	else
	   max = b;
	diff = c - max;

IF statement with single then-statement, no else:

	C					FORTRAN
	-------					-------
	max = a;				MAX = A
	if ( b > a )				IF ( B .GT. A ) MAX = B
	   max = b;				DIFF = C - MAX
	diff = c - max;

Simulating a block (reverse condition), no else:

	if ( a > b )        			IF ( A .LE. B ) GOTO ENDIF
	{                    			TEMP = A
	    temp = a;     			A = B
	    a = b;           			B = TEMP
	    b = temp;   		ENDIF:	DIFF = C - B
	}
	diff = c - b;

IF statement with single then-statement, single else statement:

	if ( a > b )
	   max = a;
	else
	   max = b;
	diff = c - max;


Straight-forward translation (note: too many GOTO's):

	if ( a > b )				IF ( A .GT. B ) GOTO THEN
	   max = a;				GOTO ELSE
	else				THEN:   MAX = A
	   max = b;			        GOTO ENDIF
	diff = c - max;			ELSE:   MAX = B
	               			ENDIF:	DIFF = C - MAX

Reverse the condition:

	if ( a > b )				IF ( A .LE. B ) GOTO ELSE
	   max = a;				MAX = A
	else					GOTO ENDIF
	   max = b;			ELSE:	MAX = B
	diff = c - max;			ENDIF:	DIFF = C - MAX

Or be clever and eliminate the else (where we started):

	max = a;				MAX = A
	if ( b > a )				IF ( B .GT. A ) MAX = B
	   max = b;				DIFF = C - MAX
	diff = c - max;

Alyce Brady, Kalamazoo College