Fortran: If statement with two commands on the same line

Viewed 1138

In fortran, in a single line, I want to execute two commands after an if, like shown below.

if (.true.) err = 'my error message' AND return

Is this possible? Using an if (.true.) then ... takes 4 whole lines!

2 Answers

An IF statement is designed and specified specifically to conditionally execute a single (action) statement.

IF statements are not unique in rejected "joined statements"; nowhere in Fortran, unlike some other languages, is it possible to perform multiple statements as though they are one. (Some statements can be rewritten so that their combined effects can be accomplished in a single statement.)

An IF construct is precisely for the case of an executable block of one or more statements. And a wider variety of activities can be performed in an IF construct which isn't restricted to executing action statements.

To reflect what has been commented so far by others:

  • an IF construct can be written on a single line (but really shouldn't be)
  • the intent is much clearer in an IF construct than in some ersatz statement joining fashion
  • Fortran is already a very verbose language and using four lines instead of one monster line shouldn't be regarded as a bad thing

@King has the answer:

if (.true.) then; err = 'my error message'; return; end if
Related