Surprising modern Fortran behaviors

Here are a couple things that may trip up a classic or modern Fortran programmer.

Single line IF

The single line IF in both classic and modern Fortran only “captures” the first statement. If you use a semicolon, the code after the semicolon is always executed!

  • Correct:

    if (.true.) x = x+1
  • Incorrect: Here y always is y+1 !!

    if (.true.) x = x+1; y=y+1

MOD() vs. MODULO()

Fortran 77 had mod(), which could output negative numbers. This is often NOT the behavior you want, as other languages’ mod() functions only output positive numbers.

So typically, use Fortran modulo().