24.11.2014 Views

Open Watcom FORTRAN 77 Language Reference

Open Watcom FORTRAN 77 Language Reference

Open Watcom FORTRAN 77 Language Reference

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Program Structure Control Statements<br />

The presence of the "..." in the above formats indicates that the ELSE IF statement may be repeated as<br />

often as desired. If the value of logical-expression-1 is true in case (a), the block of statements<br />

following the IF statement up to the first ELSE IF statement is executed, after which control passes to the<br />

statement following the END IF statement; otherwise, control will pass to the first ELSE IF statement. If<br />

the value of logical-expression-2 is true, the block of statements following the first ELSE IF<br />

statement up to the next ELSE IF statement or END IF statement is executed, after which control passes<br />

to the statement following the END IF statement; otherwise, control will pass to the next ELSE IF<br />

statement, if there is one, or directly to the statement following the END IF statement. When the ELSE<br />

statement is used, as in case (b), and the values of all the logical expressions in the IF and ELSE IF<br />

statements are false, the block of statements following the ELSE statement is executed and then control<br />

passes to the statement following the END IF statement. An optional block label may be specified with<br />

the IF statement (see the CYCLE, EXIT or QUIT statement for more information).<br />

Examples follow which illustrate the use of the two formats.<br />

Example:<br />

IF( I .EQ. 0 )THEN<br />

PRINT *, ’I IS ZERO’<br />

ELSE IF( I .GT. 0 )THEN<br />

PRINT *, ’I IS GREATER THAN ZERO’<br />

END IF<br />

If variable I is zero when the IF statement is executed, the string I IS ZERO will be printed and the<br />

statement following the END IF statement will be executed. If variable I is not zero when the IF<br />

statement is executed, control will pass to the ELSE IF statement. If variable I is greater than zero, the<br />

string I IS GREATER THAN ZERO will be printed and the statement following the END IF statement<br />

will be executed. If variable I is less than zero then nothing would be printed and control passes from the<br />

ELSE IF statement to the statement following the END IF statement.<br />

Example:<br />

IF( A .GT. B )THEN<br />

PRINT *, ’A GREATER THAN B’<br />

A = A - B<br />

ELSE IF( A .LT. B )THEN<br />

PRINT *, ’A LESS THAN B’<br />

A = B - A<br />

ELSE<br />

PRINT *, ’A EQUAL TO B’<br />

A = 0.0<br />

END IF<br />

If the value of variable A is greater than the value of variable B when the IF statement is executed, the<br />

string A GREATER THAN B will be printed and variable A will be assigned the value of the expression A<br />

- B. Control will then pass to the statement following the END IF statement.<br />

If the value of variable A is not greater than the value of variable B when the IF statement is executed,<br />

control passes to the ELSE IF statement. If the value of variable A is less than the value of variable B,<br />

the string A LESS THAN B will be printed and variable A will be assigned the value of the expression B<br />

- A. Control will then pass to the statement following the END IF statement.<br />

If the value of variable A is not less than the value of variable B when the ELSE IF statement is executed,<br />

the string A EQUAL TO B will be printed and variable A will be assigned the value zero. Control will<br />

pass to the statement following the END IF statement.<br />

ELSE IF 195

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!