“How It Works” – The Mental Model
Fortran works like this: You write instructions in a .f90 file → The compiler (gfortran) translates them to machine code → You run the resulting executable (./program_name).
Key Concept: Fortran is compiled, not interpreted. This means:
- You write code → Save as
.f90 - You compile →
gfortran file.f90 -o program - You run →
./program
📋 Quick Syntax Reference
Program Structure (MANDATORY!)
fortran
program program_name ! MUST HAVE - Start implicit none ! RECOMMENDED - Catches errors ! Your code here print *, "Hello" ! Print to screen end program program_name ! MUST HAVE - End
Variables & Data Types
fortran
! Integer (whole numbers) integer :: x, y integer :: z = 10 ! Real (decimal numbers) real :: a, b real :: pi = 3.14159 ! Double precision (more accurate) double precision :: d, e ! Character (text) character(len=20) :: name character(len=*), parameter :: greeting = "Hello" ! Logical (true/false) logical :: flag flag = .true. flag = .false. ! Arrays real, dimension(5) :: arr ! 1D array size 5 real, dimension(3,3) :: matrix ! 2D array 3x3 real :: arr2(5) ! Alternative syntax
Math Operations
fortran
! Basic math x = 5 + 3 ! Addition x = 5 - 3 ! Subtraction x = 5 * 3 ! Multiplication x = 5 / 3 ! Division (integer gives integer) x = 5.0 / 3.0 ! Real division x = 5 ** 2 ! Power (5² = 25) ! Order of operations: ( ) first, then **, then * /, then + - ! Example: x = (5 + 3) * 2 ** 2 ! = (8) * 4 = 32
Conditional Statements
fortran
! If statement
if (x > 0) then
print *, "Positive"
else if (x < 0) then
print *, "Negative"
else
print *, "Zero"
end if
! Select case (like switch)
select case (x)
case (1)
print *, "One"
case (2, 3, 4)
print *, "Two, three, or four"
case default
print *, "Other"
end select
Loops
fortran
! DO loop (count-controlled) do i = 1, 10 print *, i end do ! DO with step do i = 1, 10, 2 ! 1, 3, 5, 7, 9 print *, i end do ! DO WHILE loop do while (x < 10) x = x + 1 end do ! Exit loop early do i = 1, 100 if (i > 10) exit ! Stop at 10 print *, i end do
Arrays (MOST IMPORTANT FOR SCIENTIFIC COMPUTING)
fortran
! Array operations (vectorized!) real, dimension(5) :: a, b, c a = [1.0, 2.0, 3.0, 4.0, 5.0] b = [5.0, 4.0, 3.0, 2.0, 1.0] c = a + b ! Whole array operation! ! Array slices a(3:5) = 0 ! Set elements 3-5 to 0 a(1:3) = [1,2,3] ! Set first 3 elements ! 2D arrays real :: matrix(3,3) matrix(1,1) = 1.0 ! First row, first column matrix(:,1) = 1.0 ! Entire first column matrix(2,:) = 0.0 ! Entire second row
Subprograms (Functions & Subroutines)
fortran
! Function (returns a value) function add(a, b) result(result) implicit none real, intent(in) :: a, b real :: result result = a + b end function add ! Subroutine (doesn't return a value) subroutine print_sum(a, b) implicit none real, intent(in) :: a, b print *, "Sum:", a + b end subroutine print_sum ! Calling them x = add(5.0, 3.0) call print_sum(5.0, 3.0)
Modules (Reusable Code)
fortran
module my_module
implicit none
contains
function add(a, b)
real :: add
real, intent(in) :: a, b
add = a + b
end function add
end module my_module
! Use in main program
program main
use my_module
implicit none
print *, add(5.0, 3.0)
end program main
🔑 Key Rules to Remember
| Rule | Why | Example |
|---|---|---|
Every program needs program and end program | Tells compiler where code starts/ends | program hello ... end program hello |
Use implicit none | Forces you to declare variables → catches typos! | implicit none at top |
| Strings MUST be in quotes | "text" or 'text' | print *, "Hello" |
Comments start with ! | Everything after ! is ignored | ! This is a comment |
| Arrays start at index 1 | NOT 0! Unlike Python/C | arr(1) is first element |
Use :: when declaring | Separates type from variables | integer :: x, y |
| Indentation matters for readability | NOT required, but helps humans! | Indent code inside loops/ifs |
💡 Common Commands (Bash Reference)
| Command | Purpose |
|---|---|
nano file.f90 | Open file in editor |
gfortran file.f90 -o program | Compile (create executable) |
./program | Run the program |
gfortran -O2 file.f90 -o program | Compile with optimization (faster!) |
gfortran file.f90 -o program -llapack -lblas | Link with BLAS/LAPACK |
cat file.f90 | View file contents |
ls | List files |
📚 Learning Path
Starter Programs (Learn Syntax)
- hello.f90 → Program structure
- variables.f90 → Data types
- loops.f90 → DO loops
- arrays.f90 → Array operations
- functions.f90 → Subprograms
Advanced (Scientific Computing)
- solve_linear.f90 → LAPACK for linear systems
- matrix_multiply.f90 → BLAS for matrix multiplication
- eigenvalues.f90 → LAPACK for eigenvalues
📝 Quick Reference Card
text
┌──────────────────────────────────────────────────────────┐ │ FORTRAN 90/95 CHEAT SHEET │ ├──────────────────────────────────────────────────────────┤ │ PROGRAM STRUCTURE │ │ program name │ │ implicit none │ │ ! code here │ │ end program name │ ├──────────────────────────────────────────────────────────┤ │ DATA TYPES │ │ integer, real, double precision, character, logical │ │ integer :: x │ │ real, dimension(10) :: arr │ ├──────────────────────────────────────────────────────────┤ │ CONTROL FLOW │ │ if (condition) then │ │ ! code │ │ else if (condition) then │ │ ! code │ │ else │ │ ! code │ │ end if │ │ │ │ do i = 1, 10 │ │ ! code │ │ end do │ ├──────────────────────────────────────────────────────────┤ │ PRINTING │ │ print *, "Text", variable │ │ print '(F10.4)', x ! formatted output │ ├──────────────────────────────────────────────────────────┤ │ COMPILING │ │ gfortran file.f90 -o program │ │ gfortran -O2 file.f90 -o program -llapack -lblas │ ├──────────────────────────────────────────────────────────┤ │ RUNNING │ │ ./program │ └──────────────────────────────────────────────────────────┘
🎯 Featured Resource Entry
When I return to practice Fortran, look for:
Study Resources:
- Fortran 90 Tutorial (Stanford) → Beginner-friendly, covers basics
- Fortran Wiki → Comprehensive reference, community-driven
- Quickstart Tutorial → Hands-on, practical examples
Practice Flow:
- Write code in
nanoor your preferred editor - Compile with
gfortran file.f90 -o program - Run with
./program - Check output, debug, repeat