RichardBJ
RichardBJ

Reputation: 377

Fortran if then and instruction all on the same line

I don’t speak Fortran, but am trying to recode a few functions from Fortran into Python. I can’t compile and link the whole original source (involves c libraries too) and debug the original. But I’ve become stuck on a little piece an experience coder may be able to help with. Essentially, in the middle of the Fortran code we have an if statement with instruction but ALSO a “then” on the SAME line:

If (something) then instruction
Something
Something etc.

this is nested in a series of ‘if end if’ blocks. I know the syntax of this is not recommended/allowed, but the program apparently ran fine. My Python version does not, so this could be the source of my error. So my question is; do I just ignore the “then” treat it as

IF (something) instruction
Something
Something etc

OR alternatively, should I be looking for a closing ‘end if’ in this situation? Is that even clear what the problem is? Actual code, as suggested here: Line in question is:

if (t.lt.1.) then i=i-1

All indentation as published..

subroutine detpic(sen,der,i)
     dimension sen(100000),der(100000)
     logical detmax,derpos

     detmax=.false.
     do while (der(i).eq.0.)
          i=i+1 
          if (i.gt.39999) then
              return
          end if
     end do
     if (der(i).gt.0.) then
                    derpos=.true.
                       else
                    derpos=.false.
     end if
     do while (detmax.eqv..false.)
          if (derpos.eqv..true.) then
              do while (der(i).ge.0.)
                  i=i+1
                  if (i.gt.39999) then
                      return
                  end if
              end do
                  t=der(i-1)/(-der(i))
                  if (t.lt.1.) then i=i-1
                  detmax=.true.
                              else
              do while (der(i).le.0)
                  i=i+1
                  if (i.gt.39999) then
                      return
                  end if
              end do
              derpos=.true.
          end if
     end do
     return       
     end

Upvotes: 3

Views: 927

Answers (1)

francescalus
francescalus

Reputation: 32366

The statement like

      if (t.lt.1.) then i=i-1

is a valid Fortran IF statement if the source is in fixed form (and only if in fixed form). It is the same statement as

      if (t.lt.1.) theni=i-1

which is a conditional assignment to the variable theni. This is yet another time where implicit none and using free-form source makes code a lot easier to debug and understand.

If free-form source this is not a valid IF statement or IF construct introduction.

More generally, no action statement, other than an assignment statement or pointer assignment statement, begins with the four characters then. Equally no statement other than an IF or IF-THEN statement begins with if(expression) then. Together, these mean that the only interpretation of an if (condition) then something (for non-whitespace something) is as an assignment or pointer assignment action statement in an IF statement.

Upvotes: 5

Related Questions