Skipping lines in Fortran

Viewed 106

What lines are read in an Fortran IF-statement? I wondered which of these three lines will be read depending on the IF-statement outcome:

    IF (x.GT.y) GOTO 100
    x= y+1
100 x = y

Will the indexed row 100 always be read? Or is it skipped if the IF-statement returns True and x= y+1 is stored?

1 Answers

The three lines in the example form three distinct executable statements. They are not part of a single IF statement.

The IF statement is formed from the single (non-continued) line

IF (x.GT.y) GOTO 100

Once the IF statement has been executed, the flow of execution either moves on to the next line/statement (if the condition is false) or jumps to the statement labelled 100 (if true). Once execution has flowed, the IF statement exerts no further influence.

If execution reaches the second statement there is no further flow control to stop execution reaching the third statement (other than any defined assignment, defined + operator, or error caused by the evaluation of the right-hand side, etc).

On reaching the third line:

  • recall that the IF statement is in the long-forgotten past: the label 100 and the "jump target" are irrelevant to the present
  • the statement having a label does not change how the statement is evaluated (just how it can be reached).

That is, there is nothing which says "this line should be treated in a special way".

Yes, the third line will ("always") be executed in full.

Related