Fortran Cheat Sheet
Core modern Fortran syntax covering variable declarations, control flow, arrays, and procedures for scientific and numerical computing.
Program Structure & Variables
Basic program layout and variable declarations.
program hello implicit none ! always use this integer :: i, j real :: x, y real(kind=8) :: z ! double precision character(len=20) :: name logical :: flag x = 3.14 name = 'Fortran' print *, 'Hello, World!' print *, 'x =', xend program hello
Control Flow
If blocks, select case, and loops.
if (x > 0) then print *, 'positive'else if (x < 0) then print *, 'negative'else print *, 'zero'end ifselect case (n)case (1) print *, 'one'case (2:5) print *, 'two to five'case default print *, 'other'end selectdo i = 1, 10 print *, iend dodo while (x < 10.0) x = x + 1.0end do
Arrays
Array declarations, slicing, and array intrinsics.
integer :: arr(10)integer, dimension(3,3) :: matrixreal, allocatable :: dyn(:)arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]arr(1:5) = 0 ! slice assignmentallocate(dyn(100))print *, sum(arr) ! sum of all elementsprint *, size(arr) ! number of elementsprint *, maxval(arr) ! largest elementdeallocate(dyn)
Subroutines, Functions & Modules
Defining reusable procedures and organizing code into modules.
module mymod implicit nonecontains function square(x) result(y) real, intent(in) :: x real :: y y = x * x end function square subroutine greet(name) character(len=*), intent(in) :: name print *, 'Hello, ', name end subroutine greetend module mymodprogram main use mymod implicit none call greet('World') print *, square(4.0)end program main
Derived Types
User-defined composite types with components and type-bound procedures.
type :: point real :: x, ycontains procedure :: norm => point_normend type pointtype(point) :: pp = point(3.0, 4.0) ! structure constructorprint *, p%x, p%y ! component accessreal function point_norm(self) class(point), intent(in) :: self point_norm = sqrt(self%x**2 + self%y**2)end function point_norm
Allocatable Arrays & Pointers
Dynamic memory with allocate/deallocate and pointer association.
real, allocatable :: a(:)integer :: n = 100, ierrallocate(a(n), stat=ierr)if (ierr /= 0) stop 'alloc failed'a = 0.0if (allocated(a)) deallocate(a)real, target :: t(10)real, pointer :: p(:)p => t(2:6) ! pointer to a slicenullify(p)
Common Intrinsic Functions
Frequently used built-in numeric and array intrinsics.
- sum(a) / product(a)- reduction over array elements, optional dim= and mask=
- maxval / minval / maxloc- extreme value or its 1-based index in an array
- matmul(a, b)- matrix or matrix-vector multiplication
- dot_product(u, v)- inner product of two rank-1 arrays
- reshape(src, shape)- reshape an array to a new shape, column-major fill
- size / shape / lbound / ubound- query array extents and bounds
- any / all / count- logical reductions over a mask array
- trim / adjustl / index- string trimming, left-justify, and substring search
File I/O
Opening, reading, writing, and formatted output to units.
integer :: u, iosopen(newunit=u, file='data.txt', status='old', action='read', iostat=ios)if (ios /= 0) stop 'open failed'real :: xdo read(u, *, iostat=ios) x if (ios /= 0) exit ! EOF or error write(*, '(F8.3)') xend doclose(u)
Always start every program unit with `implicit none` — it forces you to declare every variable and catches typos that would otherwise silently create new real variables at compile time.