018. Fortran has a one-line if#

topic: Conditionals

In addition to if-else if, Fortran has a one-line if:

if (condition) executable_statement
if-one-line.f90 | | Godbolt Compiler Explorer logo | Fortran logo#
integer :: i

do i = -1, 1
   if (i /= 0) print *, i, 1/i  ! one-line if equivalent to below
   if (i /= 0) then
      print *, i, 1/i
   end if
end do

end
Output1#
          -1          -1
          -1          -1
           1           1
           1           1


1

Compiled using GNU Fortran (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0 with no flags